{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# \u7b2c\u5341\u7ae0\uff1a\u6a21\u5757\u4e0e\u5305\n \u6a21\u5757\u4e0e\u5305\u662f\u4efb\u4f55\u5927\u578b\u7a0b\u5e8f\u7684\u6838\u5fc3\uff0c\u5c31\u8fdePython\u5b89\u88c5\u7a0b\u5e8f\u672c\u8eab\u4e5f\u662f\u4e00\u4e2a\u5305\u3002\u672c\u7ae0\u91cd\u70b9\u6d89\u53ca\u6709\u5173\u6a21\u5757\u548c\u5305\u7684\u5e38\u7528\u7f16\u7a0b\u6280\u672f\uff0c\u4f8b\u5982\u5982\u4f55\u7ec4\u7ec7\u5305\u3001\u628a\u5927\u578b\u6a21\u5757\u5206\u5272\u6210\u591a\u4e2a\u6587\u4ef6\u3001\u521b\u5efa\u547d\u540d\u7a7a\u95f4\u5305\u3002\u540c\u65f6\uff0c\u4e5f\u7ed9\u51fa\u4e86\u8ba9\u4f60\u81ea\u5b9a\u4e49\u5bfc\u5165\u8bed\u53e5\u7684\u79d8\u7c4d\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 10.1 \u6784\u5efa\u4e00\u4e2a\u6a21\u5757\u7684\u5c42\u7ea7\u5305\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5c06\u4f60\u7684\u4ee3\u7801\u7ec4\u7ec7\u6210\u7531\u5f88\u591a\u5206\u5c42\u6a21\u5757\u6784\u6210\u7684\u5305\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c01\u88c5\u6210\u5305\u662f\u5f88\u7b80\u5355\u7684\u3002\u5728\u6587\u4ef6\u7cfb\u7edf\u4e0a\u7ec4\u7ec7\u4f60\u7684\u4ee3\u7801\uff0c\u5e76\u786e\u4fdd\u6bcf\u4e2a\u76ee\u5f55\u90fd\u5b9a\u4e49\u4e86\u4e00\u4e2a__init__.py\u6587\u4ef6\u3002\n\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "graphics/\n    __init__.py\n    primitive/\n        __init__.py\n        line.py\n        fill.py\n        text.py\n    formats/\n        __init__.py\n        png.py\n        jpg.py"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u65e6\u4f60\u505a\u5230\u4e86\u8fd9\u4e00\u70b9\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u6267\u884c\u5404\u79cdimport\u8bed\u53e5\uff0c\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import graphics.primitive.line\nfrom graphics.primitive import line\nimport graphics.formats.jpg as jpg"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5b9a\u4e49\u6a21\u5757\u7684\u5c42\u6b21\u7ed3\u6784\u5c31\u50cf\u5728\u6587\u4ef6\u7cfb\u7edf\u4e0a\u5efa\u7acb\u76ee\u5f55\u7ed3\u6784\u4e00\u6837\u5bb9\u6613\u3002\n\u6587\u4ef6__init__.py\u7684\u76ee\u7684\u662f\u8981\u5305\u542b\u4e0d\u540c\u8fd0\u884c\u7ea7\u522b\u7684\u5305\u7684\u53ef\u9009\u7684\u521d\u59cb\u5316\u4ee3\u7801\u3002\n\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5982\u679c\u4f60\u6267\u884c\u4e86\u8bed\u53e5import graphics\uff0c \u6587\u4ef6graphics/__init__.py\u5c06\u88ab\u5bfc\u5165,\u5efa\u7acbgraphics\u547d\u540d\u7a7a\u95f4\u7684\u5185\u5bb9\u3002\u50cfimport graphics.format.jpg\u8fd9\u6837\u5bfc\u5165\uff0c\u6587\u4ef6graphics/__init__.py\u548c\u6587\u4ef6graphics/formats/__init__.py\u5c06\u5728\u6587\u4ef6graphics/formats/jpg.py\u5bfc\u5165\u4e4b\u524d\u5bfc\u5165\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7edd\u5927\u90e8\u5206\u65f6\u5019\u8ba9__init__.py\u7a7a\u7740\u5c31\u597d\u3002\u4f46\u662f\u6709\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u5305\u542b\u4ee3\u7801\u3002\n\u4e3e\u4e2a\u4f8b\u5b50\uff0c__init__.py\u80fd\u591f\u7528\u6765\u81ea\u52a8\u52a0\u8f7d\u5b50\u6a21\u5757:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# graphics/formats/__init__.py\nfrom . import jpg\nfrom . import png"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u50cf\u8fd9\u6837\u4e00\u4e2a\u6587\u4ef6,\u7528\u6237\u53ef\u4ee5\u4ec5\u4ec5\u901a\u8fc7import grahpics.formats\u6765\u4ee3\u66ffimport graphics.formats.jpg\u4ee5\u53caimport graphics.formats.png\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "__init__.py\u7684\u5176\u4ed6\u5e38\u7528\u7528\u6cd5\u5305\u62ec\u5c06\u591a\u4e2a\u6587\u4ef6\u5408\u5e76\u5230\u4e00\u4e2a\u903b\u8f91\u547d\u540d\u7a7a\u95f4\uff0c\u8fd9\u5c06\u572810.4\u5c0f\u8282\u8ba8\u8bba\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u654f\u9510\u7684\u7a0b\u5e8f\u5458\u4f1a\u53d1\u73b0\uff0c\u5373\u4f7f\u6ca1\u6709__init__.py\u6587\u4ef6\u5b58\u5728\uff0cpython\u4ecd\u7136\u4f1a\u5bfc\u5165\u5305\u3002\u5982\u679c\u4f60\u6ca1\u6709\u5b9a\u4e49__init__.py\u65f6\uff0c\u5b9e\u9645\u4e0a\u521b\u5efa\u4e86\u4e00\u4e2a\u6240\u8c13\u7684\u201c\u547d\u540d\u7a7a\u95f4\u5305\u201d\uff0c\u8fd9\u5c06\u572810.5\u5c0f\u8282\u8ba8\u8bba\u3002\u4e07\u7269\u5e73\u7b49\uff0c\u5982\u679c\u4f60\u7740\u624b\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5305\u7684\u8bdd\uff0c\u5305\u542b\u4e00\u4e2a__init__.py\u6587\u4ef6\u5427\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 10.2 \u63a7\u5236\u6a21\u5757\u88ab\u5168\u90e8\u5bfc\u5165\u7684\u5185\u5bb9\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u4f7f\u7528\u2019from module import *\u2019 \u8bed\u53e5\u65f6\uff0c\u5e0c\u671b\u5bf9\u4ece\u6a21\u5757\u6216\u5305\u5bfc\u51fa\u7684\u7b26\u53f7\u8fdb\u884c\u7cbe\u786e\u63a7\u5236\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u4f60\u7684\u6a21\u5757\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf __all__ \u6765\u660e\u786e\u5730\u5217\u51fa\u9700\u8981\u5bfc\u51fa\u7684\u5185\u5bb9\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3e\u4e2a\u4f8b\u5b50:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# somemodule.py\ndef spam():\n    pass\n\ndef grok():\n    pass\n\nblah = 42\n# Only export 'spam' and 'grok'\n__all__ = ['spam', 'grok']"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c3d\u7ba1\u5f3a\u70c8\u53cd\u5bf9\u4f7f\u7528 \u2018from module import *\u2019, \u4f46\u662f\u5728\u5b9a\u4e49\u4e86\u5927\u91cf\u53d8\u91cf\u540d\u7684\u6a21\u5757\u4e2d\u9891\u7e41\u4f7f\u7528\u3002\n\u5982\u679c\u4f60\u4e0d\u505a\u4efb\u4f55\u4e8b, \u8fd9\u6837\u7684\u5bfc\u5165\u5c06\u4f1a\u5bfc\u5165\u6240\u6709\u4e0d\u4ee5\u4e0b\u5212\u7ebf\u5f00\u5934\u7684\u3002\n\u53e6\u4e00\u65b9\u9762,\u5982\u679c\u5b9a\u4e49\u4e86 __all__ , \u90a3\u4e48\u53ea\u6709\u88ab\u5217\u4e3e\u51fa\u7684\u4e1c\u897f\u4f1a\u88ab\u5bfc\u51fa\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u5c06 __all__ \u5b9a\u4e49\u6210\u4e00\u4e2a\u7a7a\u5217\u8868, \u6ca1\u6709\u4e1c\u897f\u5c06\u88ab\u5bfc\u5165\u3002\n\u5982\u679c __all__ \u5305\u542b\u672a\u5b9a\u4e49\u7684\u540d\u5b57, \u5728\u5bfc\u5165\u65f6\u5f15\u8d77AttributeError\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 10.3 \u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u540d\u5bfc\u5165\u5305\u4e2d\u5b50\u6a21\u5757\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c06\u4ee3\u7801\u7ec4\u7ec7\u6210\u5305,\u60f3\u7528import\u8bed\u53e5\u4ece\u53e6\u4e00\u4e2a\u5305\u540d\u6ca1\u6709\u786c\u7f16\u7801\u8fc7\u7684\u5305\u4e2d\u5bfc\u5165\u5b50\u6a21\u5757\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u5305\u7684\u76f8\u5bf9\u5bfc\u5165\uff0c\u4f7f\u4e00\u4e2a\u6a21\u5757\u5bfc\u5165\u540c\u4e00\u4e2a\u5305\u7684\u53e6\u4e00\u4e2a\u6a21\u5757\n\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u5728\u4f60\u7684\u6587\u4ef6\u7cfb\u7edf\u4e0a\u6709mypackage\u5305\uff0c\u7ec4\u7ec7\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "mypackage/\n    __init__.py\n    A/\n        __init__.py\n        spam.py\n        grok.py\n    B/\n        __init__.py\n        bar.py"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u6a21\u5757mypackage.A.spam\u8981\u5bfc\u5165\u540c\u76ee\u5f55\u4e0b\u7684\u6a21\u5757grok\uff0c\u5b83\u5e94\u8be5\u5305\u62ec\u7684import\u8bed\u53e5\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# mypackage/A/spam.py\nfrom . import grok"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u6a21\u5757mypackage.A.spam\u8981\u5bfc\u5165\u4e0d\u540c\u76ee\u5f55\u4e0b\u7684\u6a21\u5757B.bar\uff0c\u5b83\u5e94\u8be5\u4f7f\u7528\u7684import\u8bed\u53e5\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# mypackage/A/spam.py\nfrom ..B import bar"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e24\u4e2aimport\u8bed\u53e5\u90fd\u6ca1\u5305\u542b\u9876\u5c42\u5305\u540d\uff0c\u800c\u662f\u4f7f\u7528\u4e86spam.py\u7684\u76f8\u5bf9\u8def\u5f84\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u5305\u5185\uff0c\u65e2\u53ef\u4ee5\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u6765\u5bfc\u5165\u3002\n\u4e3e\u4e2a\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# mypackage/A/spam.py\nfrom mypackage.A import grok # OK\nfrom . import grok # OK\nimport grok # Error (not found)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u50cfmypackage.A\u8fd9\u6837\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u540d\u7684\u4e0d\u5229\u4e4b\u5904\u662f\u8fd9\u5c06\u9876\u5c42\u5305\u540d\u786c\u7f16\u7801\u5230\u4f60\u7684\u6e90\u7801\u4e2d\u3002\u5982\u679c\u4f60\u60f3\u91cd\u65b0\u7ec4\u7ec7\u5b83\uff0c\u4f60\u7684\u4ee3\u7801\u5c06\u66f4\u8106\uff0c\u5f88\u96be\u5de5\u4f5c\u3002 \u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5982\u679c\u4f60\u6539\u53d8\u4e86\u5305\u540d\uff0c\u4f60\u5c31\u5fc5\u987b\u68c0\u67e5\u6240\u6709\u6587\u4ef6\u6765\u4fee\u6b63\u6e90\u7801\u3002 \u540c\u6837\uff0c\u786c\u7f16\u7801\u7684\u540d\u79f0\u4f1a\u4f7f\u79fb\u52a8\u4ee3\u7801\u53d8\u5f97\u56f0\u96be\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u4e5f\u8bb8\u6709\u4eba\u60f3\u5b89\u88c5\u4e24\u4e2a\u4e0d\u540c\u7248\u672c\u7684\u8f6f\u4ef6\u5305\uff0c\u53ea\u901a\u8fc7\u540d\u79f0\u533a\u5206\u5b83\u4eec\u3002 \u5982\u679c\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\uff0c\u90a3\u4e00\u5207\u90fdok\uff0c\u7136\u800c\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u540d\u5f88\u53ef\u80fd\u4f1a\u51fa\u95ee\u9898\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "import\u8bed\u53e5\u7684 . \u548c .. \u770b\u8d77\u6765\u5f88\u6ed1\u7a3d, \u4f46\u5b83\u6307\u5b9a\u76ee\u5f55\u540d.\u4e3a\u5f53\u524d\u76ee\u5f55\uff0c..B\u4e3a\u76ee\u5f55../B\u3002\u8fd9\u79cd\u8bed\u6cd5\u53ea\u9002\u7528\u4e8eimport\u3002\n\u4e3e\u4e2a\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from . import grok # OK\nimport .grok # ERROR"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c3d\u7ba1\u4f7f\u7528\u76f8\u5bf9\u5bfc\u5165\u770b\u8d77\u6765\u50cf\u662f\u6d4f\u89c8\u6587\u4ef6\u7cfb\u7edf\uff0c\u4f46\u662f\u4e0d\u80fd\u5230\u5b9a\u4e49\u5305\u7684\u76ee\u5f55\u4e4b\u5916\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u4f7f\u7528\u70b9\u7684\u8fd9\u79cd\u6a21\u5f0f\u4ece\u4e0d\u662f\u5305\u7684\u76ee\u5f55\u4e2d\u5bfc\u5165\u5c06\u4f1a\u5f15\u53d1\u9519\u8bef\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\uff0c\u76f8\u5bf9\u5bfc\u5165\u53ea\u9002\u7528\u4e8e\u5728\u5408\u9002\u7684\u5305\u4e2d\u7684\u6a21\u5757\u3002\u5c24\u5176\u662f\u5728\u9876\u5c42\u7684\u811a\u672c\u7684\u7b80\u5355\u6a21\u5757\u4e2d\uff0c\u5b83\u4eec\u5c06\u4e0d\u8d77\u4f5c\u7528\u3002\u5982\u679c\u5305\u7684\u90e8\u5206\u88ab\u4f5c\u4e3a\u811a\u672c\u76f4\u63a5\u6267\u884c\uff0c\u90a3\u5b83\u4eec\u5c06\u4e0d\u8d77\u4f5c\u7528\n\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "% python3 mypackage/A/spam.py # Relative imports fail"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53e6\u4e00\u65b9\u9762\uff0c\u5982\u679c\u4f60\u4f7f\u7528Python\u7684-m\u9009\u9879\u6765\u6267\u884c\u5148\u524d\u7684\u811a\u672c\uff0c\u76f8\u5bf9\u5bfc\u5165\u5c06\u4f1a\u6b63\u786e\u8fd0\u884c\u3002\n\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "% python3 -m mypackage.A.spam # Relative imports work"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u66f4\u591a\u7684\u5305\u7684\u76f8\u5bf9\u5bfc\u5165\u7684\u80cc\u666f\u77e5\u8bc6,\u8bf7\u770b PEP 328 ."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 10.4 \u5c06\u6a21\u5757\u5206\u5272\u6210\u591a\u4e2a\u6587\u4ef6\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5c06\u4e00\u4e2a\u6a21\u5757\u5206\u5272\u6210\u591a\u4e2a\u6587\u4ef6\u3002\u4f46\u662f\u4f60\u4e0d\u60f3\u5c06\u5206\u79bb\u7684\u6587\u4ef6\u7edf\u4e00\u6210\u4e00\u4e2a\u903b\u8f91\u6a21\u5757\u65f6\u4f7f\u5df2\u6709\u7684\u4ee3\u7801\u906d\u5230\u7834\u574f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7a0b\u5e8f\u6a21\u5757\u53ef\u4ee5\u901a\u8fc7\u53d8\u6210\u5305\u6765\u5206\u5272\u6210\u591a\u4e2a\u72ec\u7acb\u7684\u6587\u4ef6\u3002\u8003\u8651\u4e0b\u4e0b\u9762\u7b80\u5355\u7684\u6a21\u5757\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# mymodule.py\nclass A:\n    def spam(self):\n        print('A.spam')\n\nclass B(A):\n    def bar(self):\n        print('B.bar')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5047\u8bbe\u4f60\u60f3mymodule.py\u5206\u4e3a\u4e24\u4e2a\u6587\u4ef6\uff0c\u6bcf\u4e2a\u5b9a\u4e49\u7684\u4e00\u4e2a\u7c7b\u3002\u8981\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u9996\u5148\u7528mymodule\u76ee\u5f55\u6765\u66ff\u6362\u6587\u4ef6mymodule.py\u3002\n\u8fd9\u8fd9\u4e2a\u76ee\u5f55\u4e0b\uff0c\u521b\u5efa\u4ee5\u4e0b\u6587\u4ef6\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "mymodule/\n    __init__.py\n    a.py\n    b.py"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728a.py\u6587\u4ef6\u4e2d\u63d2\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# a.py\nclass A:\n    def spam(self):\n        print('A.spam')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728b.py\u6587\u4ef6\u4e2d\u63d2\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# b.py\nfrom .a import A\nclass B(A):\n    def bar(self):\n        print('B.bar')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\uff0c\u5728 __init__.py \u4e2d\uff0c\u5c062\u4e2a\u6587\u4ef6\u7c98\u5408\u5728\u4e00\u8d77\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# __init__.py\nfrom .a import A\nfrom .b import B"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u6309\u7167\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u6240\u4ea7\u751f\u7684\u5305MyModule\u5c06\u4f5c\u4e3a\u4e00\u4e2a\u5355\u4e00\u7684\u903b\u8f91\u6a21\u5757\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import mymodule\na = mymodule.A()\na.spam()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "b = mymodule.B()\nb.bar()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u4e2a\u7ae0\u8282\u4e2d\u7684\u4e3b\u8981\u95ee\u9898\u662f\u4e00\u4e2a\u8bbe\u8ba1\u95ee\u9898\uff0c\u4e0d\u7ba1\u4f60\u662f\u5426\u5e0c\u671b\u7528\u6237\u4f7f\u7528\u5f88\u591a\u5c0f\u6a21\u5757\u6216\u53ea\u662f\u4e00\u4e2a\u6a21\u5757\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5728\u4e00\u4e2a\u5927\u578b\u7684\u4ee3\u7801\u5e93\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5c06\u8fd9\u4e00\u5207\u90fd\u5206\u5272\u6210\u72ec\u7acb\u7684\u6587\u4ef6\uff0c\u8ba9\u7528\u6237\u4f7f\u7528\u5927\u91cf\u7684import\u8bed\u53e5\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from mymodule.a import A\nfrom mymodule.b import B\n..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u6837\u80fd\u5de5\u4f5c\uff0c\u4f46\u8fd9\u8ba9\u7528\u6237\u627f\u53d7\u66f4\u591a\u7684\u8d1f\u62c5\uff0c\u7528\u6237\u8981\u77e5\u9053\u4e0d\u540c\u7684\u90e8\u5206\u4f4d\u4e8e\u4f55\u5904\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5c06\u8fd9\u4e9b\u7edf\u4e00\u8d77\u6765\uff0c\u4f7f\u7528\u4e00\u6761import\u5c06\u66f4\u52a0\u5bb9\u6613\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from mymodule import A, B"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u540e\u8005\u800c\u8a00\uff0c\u8ba9mymodule\u6210\u4e3a\u4e00\u4e2a\u5927\u7684\u6e90\u6587\u4ef6\u662f\u6700\u5e38\u89c1\u7684\u3002\u4f46\u662f\uff0c\u8fd9\u4e00\u7ae0\u8282\u5c55\u793a\u4e86\u5982\u4f55\u5408\u5e76\u591a\u4e2a\u6587\u4ef6\u5408\u5e76\u6210\u4e00\u4e2a\u5355\u4e00\u7684\u903b\u8f91\u547d\u540d\u7a7a\u95f4\u3002\n\u8fd9\u6837\u505a\u7684\u5173\u952e\u662f\u521b\u5efa\u4e00\u4e2a\u5305\u76ee\u5f55\uff0c\u4f7f\u7528 __init__.py \u6587\u4ef6\u6765\u5c06\u6bcf\u90e8\u5206\u7c98\u5408\u5728\u4e00\u8d77\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u4e00\u4e2a\u6a21\u5757\u88ab\u5206\u5272\uff0c\u4f60\u9700\u8981\u7279\u522b\u6ce8\u610f\u4ea4\u53c9\u5f15\u7528\u7684\u6587\u4ef6\u540d\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5728\u8fd9\u4e00\u7ae0\u8282\u4e2d\uff0cB\u7c7b\u9700\u8981\u8bbf\u95eeA\u7c7b\u4f5c\u4e3a\u57fa\u7c7b\u3002\u7528\u5305\u7684\u76f8\u5bf9\u5bfc\u5165 from .a import A \u6765\u83b7\u53d6\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6574\u4e2a\u7ae0\u8282\u90fd\u4f7f\u7528\u5305\u7684\u76f8\u5bf9\u5bfc\u5165\u6765\u907f\u514d\u5c06\u9876\u5c42\u6a21\u5757\u540d\u786c\u7f16\u7801\u5230\u6e90\u4ee3\u7801\u4e2d\u3002\u8fd9\u4f7f\u5f97\u91cd\u547d\u540d\u6a21\u5757\u6216\u8005\u5c06\u5b83\u79fb\u52a8\u5230\u522b\u7684\u4f4d\u7f6e\u66f4\u5bb9\u6613\u3002\uff08\u89c110.3\u5c0f\u8282\uff09"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f5c\u4e3a\u8fd9\u4e00\u7ae0\u8282\u7684\u5ef6\u4f38\uff0c\u5c06\u4ecb\u7ecd\u5ef6\u8fdf\u5bfc\u5165\u3002\u5982\u56fe\u6240\u793a\uff0c__init__.py\u6587\u4ef6\u4e00\u6b21\u5bfc\u5165\u6240\u6709\u5fc5\u9700\u7684\u7ec4\u4ef6\u7684\u3002\u4f46\u662f\u5bf9\u4e8e\u4e00\u4e2a\u5f88\u5927\u7684\u6a21\u5757\uff0c\u53ef\u80fd\u4f60\u53ea\u60f3\u7ec4\u4ef6\u5728\u9700\u8981\u65f6\u88ab\u52a0\u8f7d\u3002\n\u8981\u505a\u5230\u8fd9\u4e00\u70b9\uff0c__init__.py\u6709\u7ec6\u5fae\u7684\u53d8\u5316\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# __init__.py\ndef A():\n    from .a import A\n    return A()\n\ndef B():\n    from .b import B\n    return B()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u4e2a\u7248\u672c\u4e2d\uff0c\u7c7bA\u548c\u7c7bB\u88ab\u66ff\u6362\u4e3a\u5728\u7b2c\u4e00\u6b21\u8bbf\u95ee\u65f6\u52a0\u8f7d\u6240\u9700\u7684\u7c7b\u7684\u51fd\u6570\u3002\u5bf9\u4e8e\u7528\u6237\uff0c\u8fd9\u770b\u8d77\u6765\u4e0d\u4f1a\u6709\u592a\u5927\u7684\u4e0d\u540c\u3002\n\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import mymodule\na = mymodule.A()\na.spam()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5ef6\u8fdf\u52a0\u8f7d\u7684\u4e3b\u8981\u7f3a\u70b9\u662f\u7ee7\u627f\u548c\u7c7b\u578b\u68c0\u67e5\u53ef\u80fd\u4f1a\u4e2d\u65ad\u3002\u4f60\u53ef\u80fd\u4f1a\u7a0d\u5fae\u6539\u53d8\u4f60\u7684\u4ee3\u7801\uff0c\u4f8b\u5982:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "if isinstance(x, mymodule.A): # Error\n...\n\nif isinstance(x, mymodule.a.A): # Ok\n..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5ef6\u8fdf\u52a0\u8f7d\u7684\u771f\u5b9e\u4f8b\u5b50, \u89c1\u6807\u51c6\u5e93 multiprocessing/__init__.py \u7684\u6e90\u7801."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 10.5 \u5229\u7528\u547d\u540d\u7a7a\u95f4\u5bfc\u5165\u76ee\u5f55\u5206\u6563\u7684\u4ee3\u7801\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u53ef\u80fd\u6709\u5927\u91cf\u7684\u4ee3\u7801\uff0c\u7531\u4e0d\u540c\u7684\u4eba\u6765\u5206\u6563\u5730\u7ef4\u62a4\u3002\u6bcf\u4e2a\u90e8\u5206\u88ab\u7ec4\u7ec7\u4e3a\u6587\u4ef6\u76ee\u5f55\uff0c\u5982\u4e00\u4e2a\u5305\u3002\u7136\u800c\uff0c\u4f60\u5e0c\u671b\u80fd\u7528\u5171\u540c\u7684\u5305\u524d\u7f00\u5c06\u6240\u6709\u7ec4\u4ef6\u8fde\u63a5\u8d77\u6765\uff0c\u4e0d\u662f\u5c06\u6bcf\u4e00\u4e2a\u90e8\u5206\u4f5c\u4e3a\u72ec\u7acb\u7684\u5305\u6765\u5b89\u88c5\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4ece\u672c\u8d28\u4e0a\u8bb2\uff0c\u4f60\u8981\u5b9a\u4e49\u4e00\u4e2a\u9876\u7ea7Python\u5305\uff0c\u4f5c\u4e3a\u4e00\u4e2a\u5927\u96c6\u5408\u5206\u5f00\u7ef4\u62a4\u5b50\u5305\u7684\u547d\u540d\u7a7a\u95f4\u3002\u8fd9\u4e2a\u95ee\u9898\u7ecf\u5e38\u51fa\u73b0\u5728\u5927\u7684\u5e94\u7528\u6846\u67b6\u4e2d\uff0c\u6846\u67b6\u5f00\u53d1\u8005\u5e0c\u671b\u9f13\u52b1\u7528\u6237\u53d1\u5e03\u63d2\u4ef6\u6216\u9644\u52a0\u5305\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u7edf\u4e00\u4e0d\u540c\u7684\u76ee\u5f55\u91cc\u7edf\u4e00\u76f8\u540c\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u4f46\u662f\u8981\u5220\u53bb\u7528\u6765\u5c06\u7ec4\u4ef6\u8054\u5408\u8d77\u6765\u7684__init__.py\u6587\u4ef6\u3002\u5047\u8bbe\u4f60\u6709Python\u4ee3\u7801\u7684\u4e24\u4e2a\u4e0d\u540c\u7684\u76ee\u5f55\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "foo-package/\n    spam/\n        blah.py\n\nbar-package/\n    spam/\n        grok.py"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd92\u4e2a\u76ee\u5f55\u91cc\uff0c\u90fd\u6709\u7740\u5171\u540c\u7684\u547d\u540d\u7a7a\u95f4spam\u3002\u5728\u4efb\u4f55\u4e00\u4e2a\u76ee\u5f55\u91cc\u90fd\u6ca1\u6709__init__.py\u6587\u4ef6\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8ba9\u6211\u4eec\u770b\u770b\uff0c\u5982\u679c\u5c06foo-package\u548cbar-package\u90fd\u52a0\u5230python\u6a21\u5757\u8def\u5f84\u5e76\u5c1d\u8bd5\u5bfc\u5165\u4f1a\u53d1\u751f\u4ec0\u4e48"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sys\nsys.path.extend(['foo-package', 'bar-package'])\nimport spam.blah\nimport spam.grok"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e24\u4e2a\u4e0d\u540c\u7684\u5305\u76ee\u5f55\u88ab\u5408\u5e76\u5230\u4e00\u8d77\uff0c\u4f60\u53ef\u4ee5\u5bfc\u5165spam.blah\u548cspam.grok\uff0c\u5e76\u4e14\u5b83\u4eec\u80fd\u591f\u5de5\u4f5c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u91cc\u5de5\u4f5c\u7684\u673a\u5236\u88ab\u79f0\u4e3a\u201c\u5305\u547d\u540d\u7a7a\u95f4\u201d\u7684\u4e00\u4e2a\u7279\u5f81\u3002\u4ece\u672c\u8d28\u4e0a\u8bb2\uff0c\u5305\u547d\u540d\u7a7a\u95f4\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u5c01\u88c5\u8bbe\u8ba1\uff0c\u4e3a\u5408\u5e76\u4e0d\u540c\u7684\u76ee\u5f55\u7684\u4ee3\u7801\u5230\u4e00\u4e2a\u5171\u540c\u7684\u547d\u540d\u7a7a\u95f4\u3002\u5bf9\u4e8e\u5927\u7684\u6846\u67b6\uff0c\u8fd9\u53ef\u80fd\u662f\u6709\u7528\u7684\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u4e00\u4e2a\u6846\u67b6\u7684\u90e8\u5206\u88ab\u5355\u72ec\u5730\u5b89\u88c5\u4e0b\u8f7d\u3002\u5b83\u4e5f\u4f7f\u4eba\u4eec\u80fd\u591f\u8f7b\u677e\u5730\u4e3a\u8fd9\u6837\u7684\u6846\u67b6\u7f16\u5199\u7b2c\u4e09\u65b9\u9644\u52a0\u7ec4\u4ef6\u548c\u5176\u4ed6\u6269\u5c55\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5305\u547d\u540d\u7a7a\u95f4\u7684\u5173\u952e\u662f\u786e\u4fdd\u9876\u7ea7\u76ee\u5f55\u4e2d\u6ca1\u6709__init__.py\u6587\u4ef6\u6765\u4f5c\u4e3a\u5171\u540c\u7684\u547d\u540d\u7a7a\u95f4\u3002\u7f3a\u5931__init__.py\u6587\u4ef6\u4f7f\u5f97\u5728\u5bfc\u5165\u5305\u7684\u65f6\u5019\u4f1a\u53d1\u751f\u6709\u8da3\u7684\u4e8b\u60c5\uff1a\u8fd9\u5e76\u6ca1\u6709\u4ea7\u751f\u9519\u8bef\uff0c\u89e3\u91ca\u5668\u521b\u5efa\u4e86\u4e00\u4e2a\u7531\u6240\u6709\u5305\u542b\u5339\u914d\u5305\u540d\u7684\u76ee\u5f55\u7ec4\u6210\u7684\u5217\u8868\u3002\u7279\u6b8a\u7684\u5305\u547d\u540d\u7a7a\u95f4\u6a21\u5757\u88ab\u521b\u5efa\uff0c\u53ea\u8bfb\u7684\u76ee\u5f55\u5217\u8868\u526f\u672c\u88ab\u5b58\u50a8\u5728\u5176__path__\u53d8\u91cf\u4e2d\u3002\n\u4e3e\u4e2a\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import spam\nspam.__path__"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u5b9a\u4f4d\u5305\u7684\u5b50\u7ec4\u4ef6\u65f6\uff0c\u76ee\u5f55__path__\u5c06\u88ab\u7528\u5230(\u4f8b\u5982, \u5f53\u5bfc\u5165spam.grok\u6216\u8005spam.blah\u7684\u65f6\u5019)."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5305\u547d\u540d\u7a7a\u95f4\u7684\u4e00\u4e2a\u91cd\u8981\u7279\u70b9\u662f\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u7528\u81ea\u5df1\u7684\u4ee3\u7801\u6765\u6269\u5c55\u547d\u540d\u7a7a\u95f4\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u4f60\u81ea\u5df1\u7684\u4ee3\u7801\u76ee\u5f55\u50cf\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "my-package/\n    spam/\n        custom.py"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u5c06\u4f60\u7684\u4ee3\u7801\u76ee\u5f55\u548c\u5176\u4ed6\u5305\u4e00\u8d77\u6dfb\u52a0\u5230sys.path\uff0c\u8fd9\u5c06\u65e0\u7f1d\u5730\u5408\u5e76\u5230\u522b\u7684spam\u5305\u76ee\u5f55\u4e2d\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import spam.custom\nimport spam.grok\nimport spam.blah"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u4e2a\u5305\u662f\u5426\u88ab\u4f5c\u4e3a\u4e00\u4e2a\u5305\u547d\u540d\u7a7a\u95f4\u7684\u4e3b\u8981\u65b9\u6cd5\u662f\u68c0\u67e5\u5176__file__\u5c5e\u6027\u3002\u5982\u679c\u6ca1\u6709\uff0c\u90a3\u5305\u662f\u4e2a\u547d\u540d\u7a7a\u95f4\u3002\u8fd9\u4e5f\u53ef\u4ee5\u7531\u5176\u5b57\u7b26\u8868\u73b0\u5f62\u5f0f\u4e2d\u7684\u201cnamespace\u201d\u8fd9\u4e2a\u8bcd\u4f53\u73b0\u51fa\u6765\u3002"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "spam.__file__"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "spam"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u66f4\u591a\u7684\u5305\u547d\u540d\u7a7a\u95f4\u4fe1\u606f\u53ef\u4ee5\u67e5\u770b\nPEP 420."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 10.6 \u91cd\u65b0\u52a0\u8f7d\u6a21\u5757\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u91cd\u65b0\u52a0\u8f7d\u5df2\u7ecf\u52a0\u8f7d\u7684\u6a21\u5757\uff0c\u56e0\u4e3a\u4f60\u5bf9\u5176\u6e90\u7801\u8fdb\u884c\u4e86\u4fee\u6539\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528imp.reload()\u6765\u91cd\u65b0\u52a0\u8f7d\u5148\u524d\u52a0\u8f7d\u7684\u6a21\u5757\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import spam\nimport imp\nimp.reload(spam)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u91cd\u65b0\u52a0\u8f7d\u6a21\u5757\u5728\u5f00\u53d1\u548c\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\u5e38\u5e38\u5f88\u6709\u7528\u3002\u4f46\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u7684\u4ee3\u7801\u4f7f\u7528\u4f1a\u4e0d\u5b89\u5168\uff0c\u56e0\u4e3a\u5b83\u5e76\u4e0d\u603b\u662f\u50cf\u60a8\u671f\u671b\u7684\u90a3\u6837\u5de5\u4f5c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "reload()\u64e6\u9664\u4e86\u6a21\u5757\u5e95\u5c42\u5b57\u5178\u7684\u5185\u5bb9\uff0c\u5e76\u901a\u8fc7\u91cd\u65b0\u6267\u884c\u6a21\u5757\u7684\u6e90\u4ee3\u7801\u6765\u5237\u65b0\u5b83\u3002\u6a21\u5757\u5bf9\u8c61\u672c\u8eab\u7684\u8eab\u4efd\u4fdd\u6301\u4e0d\u53d8\u3002\u56e0\u6b64\uff0c\u8be5\u64cd\u4f5c\u5728\u7a0b\u5e8f\u4e2d\u6240\u6709\u5df2\u7ecf\u88ab\u5bfc\u5165\u4e86\u7684\u5730\u65b9\u66f4\u65b0\u4e86\u6a21\u5757\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c3d\u7ba1\u5982\u6b64\uff0creload()\u6ca1\u6709\u66f4\u65b0\u50cf\u201dfrom module import name\u201d\u8fd9\u6837\u4f7f\u7528import\u8bed\u53e5\u5bfc\u5165\u7684\u5b9a\u4e49\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# spam.py\ndef bar():\n    print('bar')\n\ndef grok():\n    print('grok')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u73b0\u5728\u542f\u52a8\u4ea4\u4e92\u5f0f\u4f1a\u8bdd\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import spam\nfrom spam import grok\nspam.bar()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "grok()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0d\u9000\u51faPython\u4fee\u6539spam.py\u7684\u6e90\u7801\uff0c\u5c06grok()\u51fd\u6570\u6539\u6210\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def grok():\n    print('New grok')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u73b0\u5728\u56de\u5230\u4ea4\u4e92\u5f0f\u4f1a\u8bdd\uff0c\u91cd\u65b0\u52a0\u8f7d\u6a21\u5757\uff0c\u5c1d\u8bd5\u4e0b\u8fd9\u4e2a\u5b9e\u9a8c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import imp\nimp.reload(spam)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "spam.bar()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "grok() # Notice old output"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "spam.grok() # Notice new output"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f60\u770b\u5230\u67092\u4e2a\u7248\u672c\u7684grok()\u51fd\u6570\u88ab\u52a0\u8f7d\u3002\u901a\u5e38\u6765\u8bf4\uff0c\u8fd9\u4e0d\u662f\u4f60\u60f3\u8981\u7684\uff0c\u800c\u662f\u4ee4\u4eba\u5934\u75bc\u7684\u4e8b\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u56e0\u6b64\uff0c\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u53ef\u80fd\u9700\u8981\u907f\u514d\u91cd\u65b0\u52a0\u8f7d\u6a21\u5757\u3002\u5728\u4ea4\u4e92\u73af\u5883\u4e0b\u8c03\u8bd5\uff0c\u89e3\u91ca\u7a0b\u5e8f\u5e76\u8bd5\u56fe\u5f04\u61c2\u5b83\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 10.7 \u8fd0\u884c\u76ee\u5f55\u6216\u538b\u7f29\u6587\u4ef6\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u60a8\u6709\u4e00\u4e2a\u5df2\u6210\u957f\u4e3a\u5305\u542b\u591a\u4e2a\u6587\u4ef6\u7684\u5e94\u7528\uff0c\u5b83\u5df2\u8fdc\u4e0d\u518d\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u811a\u672c\uff0c\u4f60\u60f3\u5411\u7528\u6237\u63d0\u4f9b\u4e00\u4e9b\u7b80\u5355\u7684\u65b9\u6cd5\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u5df2\u7ecf\u6709\u591a\u4e2a\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u628a\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u653e\u8fdb\u5b83\u81ea\u5df1\u7684\u76ee\u5f55\u5e76\u6dfb\u52a0\u4e00\u4e2a__main__.py\u6587\u4ef6\u3002 \u4e3e\u4e2a\u4f8b\u5b50\uff0c\u4f60\u53ef\u4ee5\u50cf\u8fd9\u6837\u521b\u5efa\u76ee\u5f55\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "myapplication/\n    spam.py\n    bar.py\n    grok.py\n    __main__.py"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c__main__.py\u5b58\u5728\uff0c\u4f60\u53ef\u4ee5\u7b80\u5355\u5730\u5728\u9876\u7ea7\u76ee\u5f55\u8fd0\u884cPython\u89e3\u91ca\u5668\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "bash % python3 myapplication"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u89e3\u91ca\u5668\u5c06\u6267\u884c__main__.py\u6587\u4ef6\u4f5c\u4e3a\u4e3b\u7a0b\u5e8f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u5c06\u4f60\u7684\u4ee3\u7801\u6253\u5305\u6210zip\u6587\u4ef6\uff0c\u8fd9\u79cd\u6280\u672f\u540c\u6837\u4e5f\u9002\u7528\uff0c\u4e3e\u4e2a\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "bash % ls\nspam.py bar.py grok.py __main__.py\nbash % zip -r myapp.zip *.py\nbash % python3 myapp.zip\n... output from __main__.py ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u521b\u5efa\u4e00\u4e2a\u76ee\u5f55\u6216zip\u6587\u4ef6\u5e76\u6dfb\u52a0__main__.py\u6587\u4ef6\u6765\u5c06\u4e00\u4e2a\u66f4\u5927\u7684Python\u5e94\u7528\u6253\u5305\u662f\u53ef\u884c\u7684\u3002\u8fd9\u548c\u4f5c\u4e3a\u6807\u51c6\u5e93\u88ab\u5b89\u88c5\u5230Python\u5e93\u7684\u4ee3\u7801\u5305\u662f\u6709\u4e00\u70b9\u533a\u522b\u7684\u3002\u76f8\u53cd\uff0c\u8fd9\u53ea\u662f\u8ba9\u522b\u4eba\u6267\u884c\u7684\u4ee3\u7801\u5305\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7531\u4e8e\u76ee\u5f55\u548czip\u6587\u4ef6\u4e0e\u6b63\u5e38\u6587\u4ef6\u6709\u4e00\u70b9\u4e0d\u540c\uff0c\u4f60\u53ef\u80fd\u8fd8\u9700\u8981\u589e\u52a0\u4e00\u4e2ashell\u811a\u672c\uff0c\u4f7f\u6267\u884c\u66f4\u52a0\u5bb9\u6613\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4ee3\u7801\u6587\u4ef6\u540d\u4e3amyapp.zip\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u8fd9\u6837\u4e00\u4e2a\u9876\u7ea7\u811a\u672c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "#!/usr/bin/env python3 /usr/local/bin/myapp.zip"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 10.8 \u8bfb\u53d6\u4f4d\u4e8e\u5305\u4e2d\u7684\u6570\u636e\u6587\u4ef6\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u7684\u5305\u4e2d\u5305\u542b\u4ee3\u7801\u9700\u8981\u53bb\u8bfb\u53d6\u7684\u6570\u636e\u6587\u4ef6\u3002\u4f60\u9700\u8981\u5c3d\u53ef\u80fd\u5730\u7528\u6700\u4fbf\u6377\u7684\u65b9\u5f0f\u6765\u505a\u8fd9\u4ef6\u4e8b\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5047\u8bbe\u4f60\u7684\u5305\u4e2d\u7684\u6587\u4ef6\u7ec4\u7ec7\u6210\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "mypackage/\n    __init__.py\n    somedata.dat\n    spam.py"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u73b0\u5728\u5047\u8bbespam.py\u6587\u4ef6\u9700\u8981\u8bfb\u53d6somedata.dat\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u3002\u4f60\u53ef\u4ee5\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u5b8c\u6210\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# spam.py\nimport pkgutil\ndata = pkgutil.get_data(__package__, 'somedata.dat')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7531\u6b64\u4ea7\u751f\u7684\u53d8\u91cf\u662f\u5305\u542b\u8be5\u6587\u4ef6\u7684\u539f\u59cb\u5185\u5bb9\u7684\u5b57\u8282\u5b57\u7b26\u4e32\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u8bfb\u53d6\u6570\u636e\u6587\u4ef6\uff0c\u4f60\u53ef\u80fd\u4f1a\u503e\u5411\u4e8e\u7f16\u5199\u4f7f\u7528\u5185\u7f6e\u7684I/ O\u529f\u80fd\u7684\u4ee3\u7801\uff0c\u5982open()\u3002\u4f46\u662f\u8fd9\u79cd\u65b9\u6cd5\u4e5f\u6709\u4e00\u4e9b\u95ee\u9898\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9996\u5148\uff0c\u4e00\u4e2a\u5305\u5bf9\u89e3\u91ca\u5668\u7684\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u51e0\u4e4e\u6ca1\u6709\u63a7\u5236\u6743\u3002\u56e0\u6b64\uff0c\u7f16\u7a0b\u65f6\u4efb\u4f55I/O\u64cd\u4f5c\u90fd\u5fc5\u987b\u4f7f\u7528\u7edd\u5bf9\u6587\u4ef6\u540d\u3002\u7531\u4e8e\u6bcf\u4e2a\u6a21\u5757\u5305\u542b\u6709\u5b8c\u6574\u8def\u5f84\u7684__file__\u53d8\u91cf\uff0c\u8fd9\u5f04\u6e05\u695a\u5b83\u7684\u8def\u5f84\u4e0d\u662f\u4e0d\u53ef\u80fd\uff0c\u4f46\u5b83\u5f88\u51cc\u4e71\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7b2c\u4e8c\uff0c\u5305\u901a\u5e38\u5b89\u88c5\u4f5c\u4e3a.zip\u6216.egg\u6587\u4ef6\uff0c\u8fd9\u4e9b\u6587\u4ef6\u5e76\u4e0d\u50cf\u5728\u6587\u4ef6\u7cfb\u7edf\u4e0a\u7684\u4e00\u4e2a\u666e\u901a\u76ee\u5f55\u91cc\u90a3\u6837\u88ab\u4fdd\u5b58\u3002\u56e0\u6b64\uff0c\u4f60\u8bd5\u56fe\u7528open()\u5bf9\u4e00\u4e2a\u5305\u542b\u6570\u636e\u6587\u4ef6\u7684\u5f52\u6863\u6587\u4ef6\u8fdb\u884c\u64cd\u4f5c\uff0c\u5b83\u6839\u672c\u4e0d\u4f1a\u5de5\u4f5c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "pkgutil.get_data()\u51fd\u6570\u662f\u4e00\u4e2a\u8bfb\u53d6\u6570\u636e\u6587\u4ef6\u7684\u9ad8\u7ea7\u5de5\u5177\uff0c\u4e0d\u7528\u7ba1\u5305\u662f\u5982\u4f55\u5b89\u88c5\u4ee5\u53ca\u5b89\u88c5\u5728\u54ea\u3002\u5b83\u53ea\u662f\u5de5\u4f5c\u5e76\u5c06\u6587\u4ef6\u5185\u5bb9\u4ee5\u5b57\u8282\u5b57\u7b26\u4e32\u8fd4\u56de\u7ed9\u4f60"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "get_data()\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u5305\u542b\u5305\u540d\u7684\u5b57\u7b26\u4e32\u3002\u4f60\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5305\u540d\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7279\u6b8a\u7684\u53d8\u91cf\uff0c\u6bd4\u5982__package__\u3002\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u5305\u5185\u6587\u4ef6\u7684\u76f8\u5bf9\u540d\u79f0\u3002\u5982\u679c\u6709\u5fc5\u8981\uff0c\u53ef\u4ee5\u4f7f\u7528\u6807\u51c6\u7684Unix\u547d\u540d\u89c4\u8303\u5230\u4e0d\u540c\u7684\u76ee\u5f55\uff0c\u53ea\u8981\u6700\u540e\u7684\u76ee\u5f55\u4ecd\u7136\u4f4d\u4e8e\u5305\u4e2d\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 10.9 \u5c06\u6587\u4ef6\u5939\u52a0\u5165\u5230sys.path\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u65e0\u6cd5\u5bfc\u5165\u4f60\u7684Python\u4ee3\u7801\u56e0\u4e3a\u5b83\u6240\u5728\u7684\u76ee\u5f55\u4e0d\u5728sys.path\u91cc\u3002\u4f60\u60f3\u5c06\u6dfb\u52a0\u65b0\u76ee\u5f55\u5230Python\u8def\u5f84\uff0c\u4f46\u662f\u4e0d\u60f3\u786c\u94fe\u63a5\u5230\u4f60\u7684\u4ee3\u7801\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6709\u4e24\u79cd\u5e38\u7528\u7684\u65b9\u5f0f\u5c06\u65b0\u76ee\u5f55\u6dfb\u52a0\u5230sys.path\u3002\u7b2c\u4e00\u79cd\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528PYTHONPATH\u73af\u5883\u53d8\u91cf\u6765\u6dfb\u52a0\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sys\nsys.path"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u81ea\u5b9a\u4e49\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u8fd9\u6837\u7684\u73af\u5883\u53d8\u91cf\u53ef\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u8bbe\u7f6e\u6216\u901a\u8fc7shell\u811a\u672c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7b2c\u4e8c\u79cd\u65b9\u6cd5\u662f\u521b\u5efa\u4e00\u4e2a.pth\u6587\u4ef6\uff0c\u5c06\u76ee\u5f55\u5217\u4e3e\u51fa\u6765\uff0c\u50cf\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# myapplication.pth\n/some/dir\n/other/dir"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a.pth\u6587\u4ef6\u9700\u8981\u653e\u5728\u67d0\u4e2aPython\u7684site-packages\u76ee\u5f55\uff0c\u901a\u5e38\u4f4d\u4e8e/usr/local/lib/python3.3/site-packages \u6216\u8005 ~/.local/lib/python3.3/sitepackages\u3002\u5f53\u89e3\u91ca\u5668\u542f\u52a8\u65f6\uff0c.pth\u6587\u4ef6\u91cc\u5217\u4e3e\u51fa\u6765\u7684\u5b58\u5728\u4e8e\u6587\u4ef6\u7cfb\u7edf\u7684\u76ee\u5f55\u5c06\u88ab\u6dfb\u52a0\u5230sys.path\u3002\u5b89\u88c5\u4e00\u4e2a.pth\u6587\u4ef6\u53ef\u80fd\u9700\u8981\u7ba1\u7406\u5458\u6743\u9650\uff0c\u5982\u679c\u5b83\u88ab\u6dfb\u52a0\u5230\u7cfb\u7edf\u7ea7\u7684Python\u89e3\u91ca\u5668\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6bd4\u8d77\u8d39\u529b\u5730\u627e\u6587\u4ef6\uff0c\u4f60\u53ef\u80fd\u4f1a\u503e\u5411\u4e8e\u5199\u4e00\u4e2a\u4ee3\u7801\u624b\u52a8\u8c03\u8282sys.path\u7684\u503c\u3002\u4f8b\u5982:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sys\nsys.path.insert(0, '/some/dir')\nsys.path.insert(0, '/other/dir')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u867d\u7136\u8fd9\u80fd\u201c\u5de5\u4f5c\u201d\uff0c\u4f46\u662f\u5728\u5b9e\u8df5\u4e2d\u6781\u4e3a\u8106\u5f31\uff0c\u5e94\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u95ee\u9898\u662f\uff0c\u5b83\u5c06\u76ee\u5f55\u540d\u786c\u7f16\u7801\u5230\u4e86\u4f60\u7684\u6e90\u4ee3\u7801\u3002\u5982\u679c\u4f60\u7684\u4ee3\u7801\u88ab\u79fb\u5230\u4e00\u4e2a\u65b0\u7684\u4f4d\u7f6e\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u7ef4\u62a4\u95ee\u9898\u3002\u66f4\u597d\u7684\u505a\u6cd5\u662f\u5728\u4e0d\u4fee\u6539\u6e90\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\uff0c\u5c06path\u914d\u7f6e\u5230\u5176\u4ed6\u5730\u65b9\u3002\u5982\u679c\u60a8\u4f7f\u7528\u6a21\u5757\u7ea7\u7684\u53d8\u91cf\u6765\u7cbe\u5fc3\u6784\u9020\u4e00\u4e2a\u9002\u5f53\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u6709\u65f6\u4f60\u53ef\u4ee5\u89e3\u51b3\u786c\u7f16\u7801\u76ee\u5f55\u7684\u95ee\u9898\uff0c\u6bd4\u5982__file__\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sys\nfrom os.path import abspath, join, dirname\nsys.path.insert(0, join(abspath(dirname(__file__)), 'src'))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u5c06src\u76ee\u5f55\u6dfb\u52a0\u5230path\u91cc\uff0c\u548c\u6267\u884c\u63d2\u5165\u6b65\u9aa4\u7684\u4ee3\u7801\u5728\u540c\u4e00\u4e2a\u76ee\u5f55\u91cc\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "site-packages\u76ee\u5f55\u662f\u7b2c\u4e09\u65b9\u5305\u548c\u6a21\u5757\u5b89\u88c5\u7684\u76ee\u5f55\u3002\u5982\u679c\u4f60\u624b\u52a8\u5b89\u88c5\u4f60\u7684\u4ee3\u7801\uff0c\u5b83\u5c06\u88ab\u5b89\u88c5\u5230site-packages\u76ee\u5f55\u3002\u867d\u7136\u7528\u4e8e\u914d\u7f6epath\u7684.pth\u6587\u4ef6\u5fc5\u987b\u653e\u7f6e\u5728site-packages\u91cc\uff0c\u4f46\u5b83\u914d\u7f6e\u7684\u8def\u5f84\u53ef\u4ee5\u662f\u7cfb\u7edf\u4e0a\u4efb\u4f55\u4f60\u5e0c\u671b\u7684\u76ee\u5f55\u3002\u56e0\u6b64\uff0c\u4f60\u53ef\u4ee5\u628a\u4f60\u7684\u4ee3\u7801\u653e\u5728\u4e00\u7cfb\u5217\u4e0d\u540c\u7684\u76ee\u5f55\uff0c\u53ea\u8981\u90a3\u4e9b\u76ee\u5f55\u5305\u542b\u5728.pth\u6587\u4ef6\u91cc\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 10.10 \u901a\u8fc7\u5b57\u7b26\u4e32\u540d\u5bfc\u5165\u6a21\u5757\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5bfc\u5165\u4e00\u4e2a\u6a21\u5757\uff0c\u4f46\u662f\u6a21\u5757\u7684\u540d\u5b57\u5728\u5b57\u7b26\u4e32\u91cc\u3002\u4f60\u60f3\u5bf9\u5b57\u7b26\u4e32\u8c03\u7528\u5bfc\u5165\u547d\u4ee4\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528importlib.import_module()\u51fd\u6570\u6765\u624b\u52a8\u5bfc\u5165\u540d\u5b57\u4e3a\u5b57\u7b26\u4e32\u7ed9\u51fa\u7684\u4e00\u4e2a\u6a21\u5757\u6216\u8005\u5305\u7684\u4e00\u90e8\u5206\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import importlib\nmath = importlib.import_module('math')\nmath.sin(2)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "mod = importlib.import_module('urllib.request')\nu = mod.urlopen('http://www.python.org')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "import_module\u53ea\u662f\u7b80\u5355\u5730\u6267\u884c\u548cimport\u76f8\u540c\u7684\u6b65\u9aa4\uff0c\u4f46\u662f\u8fd4\u56de\u751f\u6210\u7684\u6a21\u5757\u5bf9\u8c61\u3002\u4f60\u53ea\u9700\u8981\u5c06\u5176\u5b58\u50a8\u5728\u4e00\u4e2a\u53d8\u91cf\uff0c\u7136\u540e\u50cf\u6b63\u5e38\u7684\u6a21\u5757\u4e00\u6837\u4f7f\u7528\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u6b63\u5728\u4f7f\u7528\u7684\u5305\uff0cimport_module()\u4e5f\u53ef\u7528\u4e8e\u76f8\u5bf9\u5bfc\u5165\u3002\u4f46\u662f\uff0c\u4f60\u9700\u8981\u7ed9\u5b83\u4e00\u4e2a\u989d\u5916\u7684\u53c2\u6570\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import importlib\n# Same as 'from . import b'\nb = importlib.import_module('.b', __package__)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528import_module()\u624b\u52a8\u5bfc\u5165\u6a21\u5757\u7684\u95ee\u9898\u901a\u5e38\u51fa\u73b0\u5728\u4ee5\u67d0\u79cd\u65b9\u5f0f\u7f16\u5199\u4fee\u6539\u6216\u8986\u76d6\u6a21\u5757\u7684\u4ee3\u7801\u65f6\u5019\u3002\u4f8b\u5982\uff0c\u4e5f\u8bb8\u4f60\u6b63\u5728\u6267\u884c\u67d0\u79cd\u81ea\u5b9a\u4e49\u5bfc\u5165\u673a\u5236\uff0c\u9700\u8981\u901a\u8fc7\u540d\u79f0\u6765\u52a0\u8f7d\u4e00\u4e2a\u6a21\u5757\uff0c\u901a\u8fc7\u8865\u4e01\u52a0\u8f7d\u4ee3\u7801\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u65e7\u7684\u4ee3\u7801\uff0c\u6709\u65f6\u4f60\u4f1a\u770b\u5230\u7528\u4e8e\u5bfc\u5165\u7684\u5185\u5efa\u51fd\u6570__import__()\u3002\u5c3d\u7ba1\u5b83\u80fd\u5de5\u4f5c\uff0c\u4f46\u662fimportlib.import_module() \u901a\u5e38\u66f4\u5bb9\u6613\u4f7f\u7528\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u81ea\u5b9a\u4e49\u5bfc\u5165\u8fc7\u7a0b\u7684\u9ad8\u7ea7\u5b9e\u4f8b\u89c110.11\u5c0f\u8282"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 10.11 \u901a\u8fc7\u94a9\u5b50\u8fdc\u7a0b\u52a0\u8f7d\u6a21\u5757\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u81ea\u5b9a\u4e49Python\u7684import\u8bed\u53e5\uff0c\u4f7f\u5f97\u5b83\u80fd\u4ece\u8fdc\u7a0b\u673a\u5668\u4e0a\u9762\u900f\u660e\u7684\u52a0\u8f7d\u6a21\u5757\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9996\u5148\u8981\u63d0\u51fa\u6765\u7684\u662f\u5b89\u5168\u95ee\u9898\u3002\u672c\u8282\u8ba8\u8bba\u7684\u601d\u60f3\u5982\u679c\u6ca1\u6709\u4e00\u4e9b\u989d\u5916\u7684\u5b89\u5168\u548c\u8ba4\u77e5\u673a\u5236\u7684\u8bdd\u4f1a\u5f88\u7cdf\u7cd5\u3002\n\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u7684\u4e3b\u8981\u76ee\u7684\u662f\u6df1\u5165\u5206\u6790Python\u7684import\u8bed\u53e5\u673a\u5236\u3002\n\u5982\u679c\u4f60\u7406\u89e3\u4e86\u672c\u8282\u5185\u90e8\u539f\u7406\uff0c\u4f60\u5c31\u80fd\u591f\u4e3a\u5176\u4ed6\u4efb\u4f55\u76ee\u7684\u800c\u81ea\u5b9a\u4e49import\u3002\n\u6709\u4e86\u8fd9\u4e9b\uff0c\u8ba9\u6211\u4eec\u7ee7\u7eed\u5411\u524d\u8d70\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u8282\u6838\u5fc3\u662f\u8bbe\u8ba1\u5bfc\u5165\u8bed\u53e5\u7684\u6269\u5c55\u529f\u80fd\u3002\u6709\u5f88\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u505a\u8fd9\u4e2a\uff0c\n\u4e0d\u8fc7\u4e3a\u4e86\u6f14\u793a\u7684\u65b9\u4fbf\uff0c\u6211\u4eec\u5f00\u59cb\u5148\u6784\u9020\u4e0b\u9762\u8fd9\u4e2aPython\u4ee3\u7801\u7ed3\u6784\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "testcode/\n    spam.py\n    fib.py\n    grok/\n        __init__.py\n        blah.py"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e9b\u6587\u4ef6\u7684\u5185\u5bb9\u5e76\u4e0d\u91cd\u8981\uff0c\u4e0d\u8fc7\u6211\u4eec\u5728\u6bcf\u4e2a\u6587\u4ef6\u4e2d\u653e\u5165\u4e86\u5c11\u91cf\u7684\u7b80\u5355\u8bed\u53e5\u548c\u51fd\u6570\uff0c\n\u8fd9\u6837\u4f60\u53ef\u4ee5\u6d4b\u8bd5\u5b83\u4eec\u5e76\u67e5\u770b\u5f53\u5b83\u4eec\u88ab\u5bfc\u5165\u65f6\u7684\u8f93\u51fa\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# spam.py\nprint(\"I'm spam\")\n\ndef hello(name):\n    print('Hello %s' % name)\n\n# fib.py\nprint(\"I'm fib\")\n\ndef fib(n):\n    if n < 2:\n        return 1\n    else:\n        return fib(n-1) + fib(n-2)\n\n# grok/__init__.py\nprint(\"I'm grok.__init__\")\n\n# grok/blah.py\nprint(\"I'm grok.blah\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u91cc\u7684\u76ee\u7684\u662f\u5141\u8bb8\u8fd9\u4e9b\u6587\u4ef6\u4f5c\u4e3a\u6a21\u5757\u88ab\u8fdc\u7a0b\u8bbf\u95ee\u3002\n\u4e5f\u8bb8\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u5c31\u662f\u5c06\u5b83\u4eec\u53d1\u5e03\u5230\u4e00\u4e2aweb\u670d\u52a1\u5668\u4e0a\u9762\u3002\u5728testcode\u76ee\u5f55\u4e2d\u50cf\u4e0b\u9762\u8fd9\u6837\u8fd0\u884cPython\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "bash % cd testcode\nbash % python3 -m http.server 15000\nServing HTTP on 0.0.0.0 port 15000 ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u670d\u52a1\u5668\u8fd0\u884c\u8d77\u6765\u540e\u518d\u542f\u52a8\u4e00\u4e2a\u5355\u72ec\u7684Python\u89e3\u91ca\u5668\u3002\n\u786e\u4fdd\u4f60\u53ef\u4ee5\u4f7f\u7528 urllib \u8bbf\u95ee\u5230\u8fdc\u7a0b\u6587\u4ef6\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from urllib.request import urlopen\nu = urlopen('http://localhost:15000/fib.py')\ndata = u.read().decode('utf-8')\nprint(data)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4ece\u8fd9\u4e2a\u670d\u52a1\u5668\u52a0\u8f7d\u6e90\u4ee3\u7801\u662f\u63a5\u4e0b\u6765\u672c\u8282\u7684\u57fa\u7840\u3002\n\u4e3a\u4e86\u66ff\u4ee3\u624b\u52a8\u7684\u901a\u8fc7 urlopen() \u6765\u6536\u96c6\u6e90\u6587\u4ef6\uff0c\n\u6211\u4eec\u901a\u8fc7\u81ea\u5b9a\u4e49import\u8bed\u53e5\u6765\u5728\u540e\u53f0\u81ea\u52a8\u5e2e\u6211\u4eec\u505a\u5230\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u52a0\u8f7d\u8fdc\u7a0b\u6a21\u5757\u7684\u7b2c\u4e00\u79cd\u65b9\u6cd5\u662f\u521b\u5efa\u4e00\u4e2a\u663e\u5f0f\u7684\u52a0\u8f7d\u51fd\u6570\u6765\u5b8c\u6210\u5b83\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import imp\nimport urllib.request\nimport sys\n\ndef load_module(url):\n    u = urllib.request.urlopen(url)\n    source = u.read().decode('utf-8')\n    mod = sys.modules.setdefault(url, imp.new_module(url))\n    code = compile(source, url, 'exec')\n    mod.__file__ = url\n    mod.__package__ = ''\n    exec(code, mod.__dict__)\n    return mod"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u51fd\u6570\u4f1a\u4e0b\u8f7d\u6e90\u4ee3\u7801\uff0c\u5e76\u4f7f\u7528 compile() \u5c06\u5176\u7f16\u8bd1\u5230\u4e00\u4e2a\u4ee3\u7801\u5bf9\u8c61\u4e2d\uff0c\n\u7136\u540e\u5728\u4e00\u4e2a\u65b0\u521b\u5efa\u7684\u6a21\u5757\u5bf9\u8c61\u7684\u5b57\u5178\u4e2d\u6765\u6267\u884c\u5b83\u3002\u4e0b\u9762\u662f\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u7684\u65b9\u5f0f\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "fib = load_module('http://localhost:15000/fib.py')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "fib.fib(10)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "spam = load_module('http://localhost:15000/spam.py')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "spam.hello('Guido')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "fib"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "spam"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6b63\u5982\u4f60\u6240\u89c1\uff0c\u5bf9\u4e8e\u7b80\u5355\u7684\u6a21\u5757\u8fd9\u4e2a\u662f\u884c\u5f97\u901a\u7684\u3002\n\u4e0d\u8fc7\u5b83\u5e76\u6ca1\u6709\u5d4c\u5165\u5230\u901a\u5e38\u7684import\u8bed\u53e5\u4e2d\uff0c\u5982\u679c\u8981\u652f\u6301\u66f4\u9ad8\u7ea7\u7684\u7ed3\u6784\u6bd4\u5982\u5305\u5c31\u9700\u8981\u66f4\u591a\u7684\u5de5\u4f5c\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u4e2a\u66f4\u9177\u7684\u505a\u6cd5\u662f\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5bfc\u5165\u5668\u3002\u7b2c\u4e00\u79cd\u65b9\u6cd5\u662f\u521b\u5efa\u4e00\u4e2a\u5143\u8def\u5f84\u5bfc\u5165\u5668\u3002\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# urlimport.py\nimport sys\nimport importlib.abc\nimport imp\nfrom urllib.request import urlopen\nfrom urllib.error import HTTPError, URLError\nfrom html.parser import HTMLParser\n\n# Debugging\nimport logging\nlog = logging.getLogger(__name__)\n\n# Get links from a given URL\ndef _get_links(url):\n    class LinkParser(HTMLParser):\n        def handle_starttag(self, tag, attrs):\n            if tag == 'a':\n                attrs = dict(attrs)\n                links.add(attrs.get('href').rstrip('/'))\n    links = set()\n    try:\n        log.debug('Getting links from %s' % url)\n        u = urlopen(url)\n        parser = LinkParser()\n        parser.feed(u.read().decode('utf-8'))\n    except Exception as e:\n        log.debug('Could not get links. %s', e)\n    log.debug('links: %r', links)\n    return links\n\nclass UrlMetaFinder(importlib.abc.MetaPathFinder):\n    def __init__(self, baseurl):\n        self._baseurl = baseurl\n        self._links = { }\n        self._loaders = { baseurl : UrlModuleLoader(baseurl) }\n\n    def find_module(self, fullname, path=None):\n        log.debug('find_module: fullname=%r, path=%r', fullname, path)\n        if path is None:\n            baseurl = self._baseurl\n        else:\n            if not path[0].startswith(self._baseurl):\n                return None\n            baseurl = path[0]\n        parts = fullname.split('.')\n        basename = parts[-1]\n        log.debug('find_module: baseurl=%r, basename=%r', baseurl, basename)\n\n        # Check link cache\n        if basename not in self._links:\n            self._links[baseurl] = _get_links(baseurl)\n\n        # Check if it's a package\n        if basename in self._links[baseurl]:\n            log.debug('find_module: trying package %r', fullname)\n            fullurl = self._baseurl + '/' + basename\n            # Attempt to load the package (which accesses __init__.py)\n            loader = UrlPackageLoader(fullurl)\n            try:\n                loader.load_module(fullname)\n                self._links[fullurl] = _get_links(fullurl)\n                self._loaders[fullurl] = UrlModuleLoader(fullurl)\n                log.debug('find_module: package %r loaded', fullname)\n            except ImportError as e:\n                log.debug('find_module: package failed. %s', e)\n                loader = None\n            return loader\n        # A normal module\n        filename = basename + '.py'\n        if filename in self._links[baseurl]:\n            log.debug('find_module: module %r found', fullname)\n            return self._loaders[baseurl]\n        else:\n            log.debug('find_module: module %r not found', fullname)\n            return None\n\n    def invalidate_caches(self):\n        log.debug('invalidating link cache')\n        self._links.clear()\n\n# Module Loader for a URL\nclass UrlModuleLoader(importlib.abc.SourceLoader):\n    def __init__(self, baseurl):\n        self._baseurl = baseurl\n        self._source_cache = {}\n\n    def module_repr(self, module):\n        return '<urlmodule %r from %r>' % (module.__name__, module.__file__)\n\n    # Required method\n    def load_module(self, fullname):\n        code = self.get_code(fullname)\n        mod = sys.modules.setdefault(fullname, imp.new_module(fullname))\n        mod.__file__ = self.get_filename(fullname)\n        mod.__loader__ = self\n        mod.__package__ = fullname.rpartition('.')[0]\n        exec(code, mod.__dict__)\n        return mod\n\n    # Optional extensions\n    def get_code(self, fullname):\n        src = self.get_source(fullname)\n        return compile(src, self.get_filename(fullname), 'exec')\n\n    def get_data(self, path):\n        pass\n\n    def get_filename(self, fullname):\n        return self._baseurl + '/' + fullname.split('.')[-1] + '.py'\n\n    def get_source(self, fullname):\n        filename = self.get_filename(fullname)\n        log.debug('loader: reading %r', filename)\n        if filename in self._source_cache:\n            log.debug('loader: cached %r', filename)\n            return self._source_cache[filename]\n        try:\n            u = urlopen(filename)\n            source = u.read().decode('utf-8')\n            log.debug('loader: %r loaded', filename)\n            self._source_cache[filename] = source\n            return source\n        except (HTTPError, URLError) as e:\n            log.debug('loader: %r failed. %s', filename, e)\n            raise ImportError(\"Can't load %s\" % filename)\n\n    def is_package(self, fullname):\n        return False\n\n# Package loader for a URL\nclass UrlPackageLoader(UrlModuleLoader):\n    def load_module(self, fullname):\n        mod = super().load_module(fullname)\n        mod.__path__ = [ self._baseurl ]\n        mod.__package__ = fullname\n\n    def get_filename(self, fullname):\n        return self._baseurl + '/' + '__init__.py'\n\n    def is_package(self, fullname):\n        return True\n\n# Utility functions for installing/uninstalling the loader\n_installed_meta_cache = { }\ndef install_meta(address):\n    if address not in _installed_meta_cache:\n        finder = UrlMetaFinder(address)\n        _installed_meta_cache[address] = finder\n        sys.meta_path.append(finder)\n        log.debug('%r installed on sys.meta_path', finder)\n\ndef remove_meta(address):\n    if address in _installed_meta_cache:\n        finder = _installed_meta_cache.pop(address)\n        sys.meta_path.remove(finder)\n        log.debug('%r removed from sys.meta_path', finder)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u4e00\u4e2a\u4ea4\u4e92\u4f1a\u8bdd\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u524d\u9762\u7684\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# importing currently fails\nimport fib"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Load the importer and retry (it works)\nimport urlimport\nurlimport.install_meta('http://localhost:15000')\nimport fib"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import spam"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import grok.blah"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "grok.blah.__file__"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u7279\u6b8a\u7684\u65b9\u6848\u4f1a\u5b89\u88c5\u4e00\u4e2a\u7279\u522b\u7684\u67e5\u627e\u5668 UrlMetaFinder \u5b9e\u4f8b\uff0c\n\u4f5c\u4e3a sys.meta_path \u4e2d\u6700\u540e\u7684\u5b9e\u4f53\u3002\n\u5f53\u6a21\u5757\u88ab\u5bfc\u5165\u65f6\uff0c\u4f1a\u4f9d\u636e sys.meta_path \u4e2d\u7684\u67e5\u627e\u5668\u5b9a\u4f4d\u6a21\u5757\u3002\n\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cUrlMetaFinder \u5b9e\u4f8b\u662f\u6700\u540e\u4e00\u4e2a\u67e5\u627e\u5668\u65b9\u6848\uff0c\n\u5f53\u6a21\u5757\u5728\u4efb\u4f55\u4e00\u4e2a\u666e\u901a\u5730\u65b9\u90fd\u627e\u4e0d\u5230\u7684\u65f6\u5019\u5c31\u89e6\u53d1\u5b83\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f5c\u4e3a\u5e38\u89c1\u7684\u5b9e\u73b0\u65b9\u6848\uff0cUrlMetaFinder \u7c7b\u5305\u88c5\u5728\u4e00\u4e2a\u7528\u6237\u6307\u5b9a\u7684URL\u4e0a\u3002\n\u5728\u5185\u90e8\uff0c\u67e5\u627e\u5668\u901a\u8fc7\u6293\u53d6\u6307\u5b9aURL\u7684\u5185\u5bb9\u6784\u5efa\u5408\u6cd5\u7684\u94fe\u63a5\u96c6\u5408\u3002\n\u5bfc\u5165\u7684\u65f6\u5019\uff0c\u6a21\u5757\u540d\u4f1a\u8ddf\u5df2\u6709\u7684\u94fe\u63a5\u4f5c\u5bf9\u6bd4\u3002\u5982\u679c\u627e\u5230\u4e86\u4e00\u4e2a\u5339\u914d\u7684\uff0c\n\u4e00\u4e2a\u5355\u72ec\u7684 UrlModuleLoader \u7c7b\u88ab\u7528\u6765\u4ece\u8fdc\u7a0b\u673a\u5668\u4e0a\u52a0\u8f7d\u6e90\u4ee3\u7801\u5e76\u521b\u5efa\u6700\u7ec8\u7684\u6a21\u5757\u5bf9\u8c61\u3002\n\u8fd9\u91cc\u7f13\u5b58\u94fe\u63a5\u7684\u4e00\u4e2a\u539f\u56e0\u662f\u907f\u514d\u4e0d\u5fc5\u8981\u7684HTTP\u8bf7\u6c42\u91cd\u590d\u5bfc\u5165\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u81ea\u5b9a\u4e49\u5bfc\u5165\u7684\u7b2c\u4e8c\u79cd\u65b9\u6cd5\u662f\u7f16\u5199\u4e00\u4e2a\u94a9\u5b50\u76f4\u63a5\u5d4c\u5165\u5230 sys.path \u53d8\u91cf\u4e2d\u53bb\uff0c\n\u8bc6\u522b\u67d0\u4e9b\u76ee\u5f55\u547d\u540d\u6a21\u5f0f\u3002\n\u5728 urlimport.py \u4e2d\u6dfb\u52a0\u5982\u4e0b\u7684\u7c7b\u548c\u652f\u6301\u51fd\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# urlimport.py\n# ... include previous code above ...\n# Path finder class for a URL\nclass UrlPathFinder(importlib.abc.PathEntryFinder):\n    def __init__(self, baseurl):\n        self._links = None\n        self._loader = UrlModuleLoader(baseurl)\n        self._baseurl = baseurl\n\n    def find_loader(self, fullname):\n        log.debug('find_loader: %r', fullname)\n        parts = fullname.split('.')\n        basename = parts[-1]\n        # Check link cache\n        if self._links is None:\n            self._links = [] # See discussion\n            self._links = _get_links(self._baseurl)\n\n        # Check if it's a package\n        if basename in self._links:\n            log.debug('find_loader: trying package %r', fullname)\n            fullurl = self._baseurl + '/' + basename\n            # Attempt to load the package (which accesses __init__.py)\n            loader = UrlPackageLoader(fullurl)\n            try:\n                loader.load_module(fullname)\n                log.debug('find_loader: package %r loaded', fullname)\n            except ImportError as e:\n                log.debug('find_loader: %r is a namespace package', fullname)\n                loader = None\n            return (loader, [fullurl])\n\n        # A normal module\n        filename = basename + '.py'\n        if filename in self._links:\n            log.debug('find_loader: module %r found', fullname)\n            return (self._loader, [])\n        else:\n            log.debug('find_loader: module %r not found', fullname)\n            return (None, [])\n\n    def invalidate_caches(self):\n        log.debug('invalidating link cache')\n        self._links = None\n\n# Check path to see if it looks like a URL\n_url_path_cache = {}\ndef handle_url(path):\n    if path.startswith(('http://', 'https://')):\n        log.debug('Handle path? %s. [Yes]', path)\n        if path in _url_path_cache:\n            finder = _url_path_cache[path]\n        else:\n            finder = UrlPathFinder(path)\n            _url_path_cache[path] = finder\n        return finder\n    else:\n        log.debug('Handle path? %s. [No]', path)\n\ndef install_path_hook():\n    sys.path_hooks.append(handle_url)\n    sys.path_importer_cache.clear()\n    log.debug('Installing handle_url')\n\ndef remove_path_hook():\n    sys.path_hooks.remove(handle_url)\n    sys.path_importer_cache.clear()\n    log.debug('Removing handle_url')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u4f7f\u7528\u8fd9\u4e2a\u8def\u5f84\u67e5\u627e\u5668\uff0c\u4f60\u53ea\u9700\u8981\u5728 sys.path \u4e2d\u52a0\u5165URL\u94fe\u63a5\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Initial import fails\nimport fib"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Install the path hook\nimport urlimport\nurlimport.install_path_hook()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Imports still fail (not on path)\nimport fib"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Add an entry to sys.path and watch it work\nimport sys\nsys.path.append('http://localhost:15000')\nimport fib"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import grok.blah"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "grok.blah.__file__"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5173\u952e\u70b9\u5c31\u662f handle_url() \u51fd\u6570\uff0c\u5b83\u88ab\u6dfb\u52a0\u5230\u4e86 sys.path_hooks \u53d8\u91cf\u4e2d\u3002\n\u5f53 sys.path \u7684\u5b9e\u4f53\u88ab\u5904\u7406\u65f6\uff0c\u4f1a\u8c03\u7528 sys.path_hooks \u4e2d\u7684\u51fd\u6570\u3002\n\u5982\u679c\u4efb\u4f55\u4e00\u4e2a\u51fd\u6570\u8fd4\u56de\u4e86\u4e00\u4e2a\u67e5\u627e\u5668\u5bf9\u8c61\uff0c\u90a3\u4e48\u8fd9\u4e2a\u5bf9\u8c61\u5c31\u88ab\u7528\u6765\u4e3a sys.path \u5b9e\u4f53\u52a0\u8f7d\u6a21\u5757\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fdc\u7a0b\u6a21\u5757\u52a0\u8f7d\u8ddf\u5176\u4ed6\u7684\u52a0\u8f7d\u4f7f\u7528\u65b9\u6cd5\u51e0\u4e4e\u662f\u4e00\u6837\u7684\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "fib"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "fib.__name__"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "fib.__file__"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import inspect\nprint(inspect.getsource(fib))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8be6\u7ec6\u8ba8\u8bba\u4e4b\u524d\uff0c\u6709\u70b9\u8981\u5f3a\u8c03\u7684\u662f\uff0cPython\u7684\u6a21\u5757\u3001\u5305\u548c\u5bfc\u5165\u673a\u5236\u662f\u6574\u4e2a\u8bed\u8a00\u4e2d\u6700\u590d\u6742\u7684\u90e8\u5206\uff0c\n\u5373\u4f7f\u7ecf\u9a8c\u4e30\u5bcc\u7684Python\u7a0b\u5e8f\u5458\u4e5f\u5f88\u5c11\u80fd\u7cbe\u901a\u5b83\u4eec\u3002\n\u6211\u5728\u8fd9\u91cc\u63a8\u8350\u4e00\u4e9b\u503c\u7684\u53bb\u8bfb\u7684\u6587\u6863\u548c\u4e66\u7c4d\uff0c\u5305\u62ec\nimportlib module\n\u548c PEP 302.\n\u6587\u6863\u5185\u5bb9\u5728\u8fd9\u91cc\u4e0d\u4f1a\u88ab\u91cd\u590d\u63d0\u5230\uff0c\u4e0d\u8fc7\u6211\u5728\u8fd9\u91cc\u4f1a\u8ba8\u8bba\u4e00\u4e9b\u6700\u91cd\u8981\u7684\u90e8\u5206\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9996\u5148\uff0c\u5982\u679c\u4f60\u60f3\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6a21\u5757\u5bf9\u8c61\uff0c\u4f7f\u7528 imp.new_module() \u51fd\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import imp\nm = imp.new_module('spam')\nm"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m.__name__"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6a21\u5757\u5bf9\u8c61\u901a\u5e38\u6709\u4e00\u4e9b\u671f\u671b\u5c5e\u6027\uff0c\u5305\u62ec __file__ \uff08\u8fd0\u884c\u6a21\u5757\u52a0\u8f7d\u8bed\u53e5\u7684\u6587\u4ef6\u540d\uff09\n\u548c __package__ (\u5305\u540d)\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5176\u6b21\uff0c\u6a21\u5757\u4f1a\u88ab\u89e3\u91ca\u5668\u7f13\u5b58\u8d77\u6765\u3002\u6a21\u5757\u7f13\u5b58\u53ef\u4ee5\u5728\u5b57\u5178 sys.modules \u4e2d\u88ab\u627e\u5230\u3002\n\u56e0\u4e3a\u6709\u4e86\u8fd9\u4e2a\u7f13\u5b58\u673a\u5236\uff0c\u901a\u5e38\u53ef\u4ee5\u5c06\u7f13\u5b58\u548c\u6a21\u5757\u7684\u521b\u5efa\u901a\u8fc7\u4e00\u4e2a\u6b65\u9aa4\u5b8c\u6210\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sys\nimport imp\nm = sys.modules.setdefault('spam', imp.new_module('spam'))\nm"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u7ed9\u5b9a\u6a21\u5757\u5df2\u7ecf\u5b58\u5728\u90a3\u4e48\u5c31\u4f1a\u76f4\u63a5\u83b7\u5f97\u5df2\u7ecf\u88ab\u521b\u5efa\u8fc7\u7684\u6a21\u5757\uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import math\nm = sys.modules.setdefault('math', imp.new_module('math'))\nm"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m.sin(2)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m.cos(2)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7531\u4e8e\u521b\u5efa\u6a21\u5757\u5f88\u7b80\u5355\uff0c\u5f88\u5bb9\u6613\u7f16\u5199\u7b80\u5355\u51fd\u6570\u6bd4\u5982\u7b2c\u4e00\u90e8\u5206\u7684 load_module() \u51fd\u6570\u3002\n\u8fd9\u4e2a\u65b9\u6848\u7684\u4e00\u4e2a\u7f3a\u70b9\u662f\u5f88\u96be\u5904\u7406\u590d\u6742\u60c5\u51b5\u6bd4\u5982\u5305\u7684\u5bfc\u5165\u3002\n\u4e3a\u4e86\u5904\u7406\u4e00\u4e2a\u5305\uff0c\u4f60\u8981\u91cd\u65b0\u5b9e\u73b0\u666e\u901aimport\u8bed\u53e5\u7684\u5e95\u5c42\u903b\u8f91\uff08\u6bd4\u5982\u68c0\u67e5\u76ee\u5f55\uff0c\u67e5\u627e__init__.py\u6587\u4ef6\uff0c\n\u6267\u884c\u90a3\u4e9b\u6587\u4ef6\uff0c\u8bbe\u7f6e\u8def\u5f84\u7b49\uff09\u3002\u8fd9\u4e2a\u590d\u6742\u6027\u5c31\u662f\u4e3a\u4ec0\u4e48\u6700\u597d\u76f4\u63a5\u6269\u5c55import\u8bed\u53e5\u800c\u4e0d\u662f\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u4e00\u4e2a\u539f\u56e0\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6269\u5c55import\u8bed\u53e5\u5f88\u7b80\u5355\uff0c\u4f46\u662f\u4f1a\u6709\u5f88\u591a\u79fb\u52a8\u64cd\u4f5c\u3002\n\u6700\u9ad8\u5c42\u4e0a\uff0c\u5bfc\u5165\u64cd\u4f5c\u88ab\u4e00\u4e2a\u4f4d\u4e8esys.meta_path\u5217\u8868\u4e2d\u7684\u201c\u5143\u8def\u5f84\u201d\u67e5\u627e\u5668\u5904\u7406\u3002\n\u5982\u679c\u4f60\u8f93\u51fa\u5b83\u7684\u503c\uff0c\u4f1a\u770b\u5230\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from pprint import pprint\npprint(sys.meta_path)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u6267\u884c\u4e00\u4e2a\u8bed\u53e5\u6bd4\u5982 import fib \u65f6\uff0c\u89e3\u91ca\u5668\u4f1a\u904d\u5386sys.mata_path\u4e2d\u7684\u67e5\u627e\u5668\u5bf9\u8c61\uff0c\n\u8c03\u7528\u5b83\u4eec\u7684 find_module() \u65b9\u6cd5\u5b9a\u4f4d\u6b63\u786e\u7684\u6a21\u5757\u52a0\u8f7d\u5668\u3002\n\u53ef\u4ee5\u901a\u8fc7\u5b9e\u9a8c\u6765\u770b\u770b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class Finder:\n    def find_module(self, fullname, path):\n        print('Looking for', fullname, path)\n        return None\nimport sys\nsys.meta_path.insert(0, Finder()) # Insert as first entry\nimport math"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import types"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import threading"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6ce8\u610f\u770b find_module() \u65b9\u6cd5\u662f\u600e\u6837\u5728\u6bcf\u4e00\u4e2a\u5bfc\u5165\u5c31\u88ab\u89e6\u53d1\u7684\u3002\n\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\u7684path\u53c2\u6570\u7684\u4f5c\u7528\u662f\u5904\u7406\u5305\u3002\n\u591a\u4e2a\u5305\u88ab\u5bfc\u5165\uff0c\u5c31\u662f\u4e00\u4e2a\u53ef\u5728\u5305\u7684 __path__ \u5c5e\u6027\u4e2d\u627e\u5230\u7684\u8def\u5f84\u5217\u8868\u3002\n\u8981\u627e\u5230\u5305\u7684\u5b50\u7ec4\u4ef6\u5c31\u8981\u68c0\u67e5\u8fd9\u4e9b\u8def\u5f84\u3002\n\u6bd4\u5982\u6ce8\u610f\u5bf9\u4e8e xml.etree \u548c xml.etree.ElementTree \u7684\u8def\u5f84\u914d\u7f6e\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import xml.etree.ElementTree"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728 sys.meta_path \u4e0a\u67e5\u627e\u5668\u7684\u4f4d\u7f6e\u5f88\u91cd\u8981\uff0c\u5c06\u5b83\u4ece\u961f\u5934\u79fb\u5230\u961f\u5c3e\uff0c\u7136\u540e\u518d\u8bd5\u8bd5\u5bfc\u5165\u770b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "del sys.meta_path[0]\nsys.meta_path.append(Finder())\nimport urllib.request\nimport datetime"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u73b0\u5728\u4f60\u770b\u4e0d\u5230\u4efb\u4f55\u8f93\u51fa\u4e86\uff0c\u56e0\u4e3a\u5bfc\u5165\u88absys.meta_path\u4e2d\u7684\u5176\u4ed6\u5b9e\u4f53\u5904\u7406\u3002\n\u8fd9\u65f6\u5019\uff0c\u4f60\u53ea\u6709\u5728\u5bfc\u5165\u4e0d\u5b58\u5728\u6a21\u5757\u7684\u65f6\u5019\u624d\u80fd\u770b\u5230\u5b83\u88ab\u89e6\u53d1\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import fib"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import xml.superfast"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u4e4b\u524d\u5b89\u88c5\u8fc7\u4e00\u4e2a\u6355\u83b7\u672a\u77e5\u6a21\u5757\u7684\u67e5\u627e\u5668\uff0c\u8fd9\u4e2a\u662f UrlMetaFinder \u7c7b\u7684\u5173\u952e\u3002\n\u4e00\u4e2a UrlMetaFinder \u5b9e\u4f8b\u88ab\u6dfb\u52a0\u5230 sys.meta_path \u7684\u672b\u5c3e\uff0c\u4f5c\u4e3a\u6700\u540e\u4e00\u4e2a\u67e5\u627e\u5668\u65b9\u6848\u3002\n\u5982\u679c\u88ab\u8bf7\u6c42\u7684\u6a21\u5757\u540d\u4e0d\u80fd\u5b9a\u4f4d\uff0c\u5c31\u4f1a\u88ab\u8fd9\u4e2a\u67e5\u627e\u5668\u5904\u7406\u6389\u3002\n\u5904\u7406\u5305\u7684\u65f6\u5019\u9700\u8981\u6ce8\u610f\uff0c\u5728path\u53c2\u6570\u4e2d\u6307\u5b9a\u7684\u503c\u9700\u8981\u88ab\u68c0\u67e5\uff0c\u770b\u5b83\u662f\u5426\u4ee5\u67e5\u627e\u5668\u4e2d\u6ce8\u518c\u7684URL\u5f00\u5934\u3002\n\u5982\u679c\u4e0d\u662f\uff0c\u8be5\u5b50\u6a21\u5757\u5fc5\u987b\u5f52\u5c5e\u4e8e\u5176\u4ed6\u67e5\u627e\u5668\u5e76\u88ab\u5ffd\u7565\u6389\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u5305\u7684\u5176\u4ed6\u5904\u7406\u53ef\u5728 UrlPackageLoader \u7c7b\u4e2d\u88ab\u627e\u5230\u3002\n\u8fd9\u4e2a\u7c7b\u4e0d\u4f1a\u5bfc\u5165\u5305\u540d\uff0c\u800c\u662f\u53bb\u52a0\u8f7d\u5bf9\u5e94\u7684 __init__.py \u6587\u4ef6\u3002\n\u5b83\u4e5f\u4f1a\u8bbe\u7f6e\u6a21\u5757\u7684 __path__ \u5c5e\u6027\uff0c\u8fd9\u4e00\u6b65\u5f88\u91cd\u8981\uff0c\n\u56e0\u4e3a\u5728\u52a0\u8f7d\u5305\u7684\u5b50\u6a21\u5757\u65f6\u8fd9\u4e2a\u503c\u4f1a\u88ab\u4f20\u7ed9\u540e\u9762\u7684 find_module() \u8c03\u7528\u3002\n\u57fa\u4e8e\u8def\u5f84\u7684\u5bfc\u5165\u94a9\u5b50\u662f\u8fd9\u4e9b\u601d\u60f3\u7684\u4e00\u4e2a\u6269\u5c55\uff0c\u4f46\u662f\u91c7\u7528\u4e86\u53e6\u5916\u7684\u65b9\u6cd5\u3002\n\u6211\u4eec\u90fd\u77e5\u9053\uff0csys.path \u662f\u4e00\u4e2aPython\u67e5\u627e\u6a21\u5757\u7684\u76ee\u5f55\u5217\u8868\uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from pprint import pprint\nimport sys\npprint(sys.path)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728 sys.path \u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b9e\u4f53\u90fd\u4f1a\u88ab\u989d\u5916\u7684\u7ed1\u5b9a\u5230\u4e00\u4e2a\u67e5\u627e\u5668\u5bf9\u8c61\u4e0a\u3002\n\u4f60\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b sys.path_importer_cache \u53bb\u770b\u4e0b\u8fd9\u4e9b\u67e5\u627e\u5668\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "pprint(sys.path_importer_cache)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "sys.path_importer_cache \u6bd4 sys.path \u4f1a\u66f4\u5927\u70b9\uff0c\n\u56e0\u4e3a\u5b83\u4f1a\u4e3a\u6240\u6709\u88ab\u52a0\u8f7d\u4ee3\u7801\u7684\u76ee\u5f55\u8bb0\u5f55\u5b83\u4eec\u7684\u67e5\u627e\u5668\u3002\n\u8fd9\u5305\u62ec\u5305\u7684\u5b50\u76ee\u5f55\uff0c\u8fd9\u4e9b\u901a\u5e38\u5728 sys.path \u4e2d\u662f\u4e0d\u5b58\u5728\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u6267\u884c import fib \uff0c\u4f1a\u987a\u5e8f\u68c0\u67e5 sys.path \u4e2d\u7684\u76ee\u5f55\u3002\n\u5bf9\u4e8e\u6bcf\u4e2a\u76ee\u5f55\uff0c\u540d\u79f0\u201cfib\u201d\u4f1a\u88ab\u4f20\u7ed9\u76f8\u5e94\u7684 sys.path_importer_cache \u4e2d\u7684\u67e5\u627e\u5668\u3002\n\u8fd9\u4e2a\u53ef\u4ee5\u8ba9\u4f60\u521b\u5efa\u81ea\u5df1\u7684\u67e5\u627e\u5668\u5e76\u5728\u7f13\u5b58\u4e2d\u653e\u5165\u4e00\u4e2a\u5b9e\u4f53\u3002\u8bd5\u8bd5\u8fd9\u4e2a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class Finder:\ndef find_loader(self, name):\n    print('Looking for', name)\n    return (None, [])\nimport sys\n# Add a \"debug\" entry to the importer cache\nsys.path_importer_cache['debug'] = Finder()\n# Add a \"debug\" directory to sys.path\nsys.path.insert(0, 'debug')\nimport threading"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u91cc\uff0c\u4f60\u53ef\u4ee5\u4e3a\u540d\u5b57\u201cdebug\u201d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7f13\u5b58\u5b9e\u4f53\u5e76\u5c06\u5b83\u8bbe\u7f6e\u6210 sys.path \u4e0a\u7684\u7b2c\u4e00\u4e2a\u3002\n\u5728\u6240\u6709\u63a5\u4e0b\u6765\u7684\u5bfc\u5165\u4e2d\uff0c\u4f60\u4f1a\u770b\u5230\u4f60\u7684\u67e5\u627e\u5668\u88ab\u89e6\u53d1\u4e86\u3002\n\u4e0d\u8fc7\uff0c\u7531\u4e8e\u5b83\u8fd4\u56de (None, [])\uff0c\u90a3\u4e48\u5904\u7406\u8fdb\u7a0b\u4f1a\u7ee7\u7eed\u5904\u7406\u4e0b\u4e00\u4e2a\u5b9e\u4f53\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "sys.path_importer_cache \u7684\u4f7f\u7528\u88ab\u4e00\u4e2a\u5b58\u50a8\u5728 sys.path_hooks \u4e2d\u7684\u51fd\u6570\u5217\u8868\u63a7\u5236\u3002\n\u8bd5\u8bd5\u4e0b\u9762\u7684\u4f8b\u5b50\uff0c\u5b83\u4f1a\u6e05\u9664\u7f13\u5b58\u5e76\u7ed9 sys.path_hooks \u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u8def\u5f84\u68c0\u67e5\u51fd\u6570"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sys.path_importer_cache.clear()\ndef check_path(path):\n    print('Checking', path)\n    raise ImportError()\nsys.path_hooks.insert(0, check_path)\nimport fib"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6b63\u5982\u4f60\u6240\u89c1\uff0ccheck_path() \u51fd\u6570\u88ab\u6bcf\u4e2a sys.path \u4e2d\u7684\u5b9e\u4f53\u8c03\u7528\u3002\n\u4e0d\u987e\uff0c\u7531\u4e8e\u629b\u51fa\u4e86 ImportError \u5f02\u5e38\uff0c\n\u5565\u90fd\u4e0d\u4f1a\u53d1\u751f\u4e86\uff08\u4ec5\u4ec5\u5c06\u68c0\u67e5\u8f6c\u79fb\u5230sys.path_hooks\u7684\u4e0b\u4e00\u4e2a\u51fd\u6570\uff09\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u77e5\u9053\u4e86\u600e\u6837sys.path\u662f\u600e\u6837\u88ab\u5904\u7406\u7684\uff0c\u4f60\u5c31\u80fd\u6784\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u8def\u5f84\u68c0\u67e5\u51fd\u6570\u6765\u67e5\u627e\u6587\u4ef6\u540d\uff0c\u4e0d\u7136URL\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def check_url(path):\n    if path.startswith('http://'):\n        return Finder()\n    else:\n        raise ImportError()\nsys.path.append('http://localhost:15000')\nsys.path_hooks[0] = check_url\nimport fib"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Notice installation of Finder in sys.path_importer_cache\nsys.path_importer_cache['http://localhost:15000']"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u5c31\u662f\u672c\u8282\u6700\u540e\u90e8\u5206\u7684\u5173\u952e\u70b9\u3002\u4e8b\u5b9e\u4e0a\uff0c\u4e00\u4e2a\u7528\u6765\u5728sys.path\u4e2d\u67e5\u627eURL\u7684\u81ea\u5b9a\u4e49\u8def\u5f84\u68c0\u67e5\u51fd\u6570\u5df2\u7ecf\u6784\u5efa\u5b8c\u6bd5\u3002\n\u5f53\u5b83\u4eec\u88ab\u78b0\u5230\u7684\u65f6\u5019\uff0c\u4e00\u4e2a\u65b0\u7684 UrlPathFinder \u5b9e\u4f8b\u88ab\u521b\u5efa\u5e76\u88ab\u653e\u5165 sys.path_importer_cache.\n\u4e4b\u540e\uff0c\u6240\u6709\u9700\u8981\u68c0\u67e5 sys.path \u7684\u5bfc\u5165\u8bed\u53e5\u90fd\u4f1a\u4f7f\u7528\u4f60\u7684\u81ea\u5b9a\u4e49\u67e5\u627e\u5668\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u57fa\u4e8e\u8def\u5f84\u5bfc\u5165\u7684\u5305\u5904\u7406\u7a0d\u5fae\u6709\u70b9\u590d\u6742\uff0c\u5e76\u4e14\u8ddf find_loader() \u65b9\u6cd5\u8fd4\u56de\u503c\u6709\u5173\u3002\n\u5bf9\u4e8e\u7b80\u5355\u6a21\u5757\uff0cfind_loader() \u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4(loader, None)\uff0c\n\u5176\u4e2d\u7684loader\u662f\u4e00\u4e2a\u7528\u4e8e\u5bfc\u5165\u6a21\u5757\u7684\u52a0\u8f7d\u5668\u5b9e\u4f8b\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u4e00\u4e2a\u666e\u901a\u7684\u5305\uff0cfind_loader() \u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4(loader, path)\uff0c\n\u5176\u4e2d\u7684loader\u662f\u4e00\u4e2a\u7528\u4e8e\u5bfc\u5165\u5305\uff08\u5e76\u6267\u884c__init__.py\uff09\u7684\u52a0\u8f7d\u5668\u5b9e\u4f8b\uff0c\npath\u662f\u4e00\u4e2a\u4f1a\u521d\u59cb\u5316\u5305\u7684 __path__ \u5c5e\u6027\u7684\u76ee\u5f55\u5217\u8868\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u57fa\u7840URL\u662f http://localhost:15000 \u5e76\u4e14\u4e00\u4e2a\u7528\u6237\u6267\u884c import grok ,\n\u90a3\u4e48 find_loader() \u8fd4\u56de\u7684path\u5c31\u4f1a\u662f [ \u2018http://localhost:15000/grok\u2019 ]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "find_loader() \u8fd8\u8981\u80fd\u5904\u7406\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u5305\u3002\n\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u5305\u4e2d\u6709\u4e00\u4e2a\u5408\u6cd5\u7684\u5305\u76ee\u5f55\u540d\uff0c\u4f46\u662f\u4e0d\u5b58\u5728__init__.py\u6587\u4ef6\u3002\n\u8fd9\u6837\u7684\u8bdd\uff0cfind_loader() \u5fc5\u987b\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4(None, path)\uff0c\npath\u662f\u4e00\u4e2a\u76ee\u5f55\u5217\u8868\uff0c\u7531\u5b83\u6765\u6784\u5efa\u5305\u7684\u5b9a\u4e49\u6709__init__.py\u6587\u4ef6\u7684__path__\u5c5e\u6027\u3002\n\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0c\u5bfc\u5165\u673a\u5236\u4f1a\u7ee7\u7eed\u524d\u884c\u53bb\u68c0\u67e5sys.path\u4e2d\u7684\u76ee\u5f55\u3002\n\u5982\u679c\u627e\u5230\u4e86\u547d\u540d\u7a7a\u95f4\u5305\uff0c\u6240\u6709\u7684\u7ed3\u679c\u8def\u5f84\u88ab\u52a0\u5230\u4e00\u8d77\u6765\u6784\u5efa\u6700\u7ec8\u7684\u547d\u540d\u7a7a\u95f4\u5305\u3002\n\u5173\u4e8e\u547d\u540d\u7a7a\u95f4\u5305\u7684\u66f4\u591a\u4fe1\u606f\u8bf7\u53c2\u800310.5\u5c0f\u8282\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6240\u6709\u7684\u5305\u90fd\u5305\u542b\u4e86\u4e00\u4e2a\u5185\u90e8\u8def\u5f84\u8bbe\u7f6e\uff0c\u53ef\u4ee5\u5728__path__\u5c5e\u6027\u4e2d\u770b\u5230\uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import xml.etree.ElementTree\nxml.__path__"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "xml.etree.__path__"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e4b\u524d\u63d0\u5230\uff0c__path__\u7684\u8bbe\u7f6e\u662f\u901a\u8fc7 find_loader() \u65b9\u6cd5\u8fd4\u56de\u503c\u63a7\u5236\u7684\u3002\n\u4e0d\u8fc7\uff0c__path__\u63a5\u4e0b\u6765\u4e5f\u88absys.path_hooks\u4e2d\u7684\u51fd\u6570\u5904\u7406\u3002\n\u56e0\u6b64\uff0c\u4f46\u5305\u7684\u5b50\u7ec4\u4ef6\u88ab\u52a0\u8f7d\u540e\uff0c\u4f4d\u4e8e__path__\u4e2d\u7684\u5b9e\u4f53\u4f1a\u88ab handle_url() \u51fd\u6570\u68c0\u67e5\u3002\n\u8fd9\u4f1a\u5bfc\u81f4\u65b0\u7684 UrlPathFinder \u5b9e\u4f8b\u88ab\u521b\u5efa\u5e76\u4e14\u88ab\u52a0\u5165\u5230 sys.path_importer_cache \u4e2d\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd8\u6709\u4e2a\u96be\u70b9\u5c31\u662f handle_url() \u51fd\u6570\u4ee5\u53ca\u5b83\u8ddf\u5185\u90e8\u4f7f\u7528\u7684 _get_links() \u51fd\u6570\u4e4b\u95f4\u7684\u4ea4\u4e92\u3002\n\u5982\u679c\u4f60\u7684\u67e5\u627e\u5668\u5b9e\u73b0\u9700\u8981\u4f7f\u7528\u5230\u5176\u4ed6\u6a21\u5757\uff08\u6bd4\u5982urllib.request\uff09\uff0c\n\u6709\u53ef\u80fd\u8fd9\u4e9b\u6a21\u5757\u4f1a\u5728\u67e5\u627e\u5668\u64cd\u4f5c\u671f\u95f4\u8fdb\u884c\u66f4\u591a\u7684\u5bfc\u5165\u3002\n\u5b83\u53ef\u4ee5\u5bfc\u81f4 handle_url() \u548c\u5176\u4ed6\u67e5\u627e\u5668\u90e8\u5206\u9677\u5165\u4e00\u79cd\u9012\u5f52\u5faa\u73af\u72b6\u6001\u3002\n\u4e3a\u4e86\u89e3\u91ca\u8fd9\u79cd\u53ef\u80fd\u6027\uff0c\u5b9e\u73b0\u4e2d\u6709\u4e00\u4e2a\u88ab\u521b\u5efa\u7684\u67e5\u627e\u5668\u7f13\u5b58\uff08\u6bcf\u4e00\u4e2aURL\u4e00\u4e2a\uff09\u3002\n\u5b83\u53ef\u4ee5\u907f\u514d\u521b\u5efa\u91cd\u590d\u67e5\u627e\u5668\u7684\u95ee\u9898\u3002\n\u53e6\u5916\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u53ef\u4ee5\u786e\u4fdd\u67e5\u627e\u5668\u4e0d\u4f1a\u5728\u521d\u59cb\u5316\u94fe\u63a5\u96c6\u5408\u7684\u65f6\u5019\u54cd\u5e94\u4efb\u4f55\u5bfc\u5165\u8bf7\u6c42\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Check link cache\nif self._links is None:\n    self._links = [] # See discussion\n    self._links = _get_links(self._baseurl)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\uff0c\u67e5\u627e\u5668\u7684 invalidate_caches() \u65b9\u6cd5\u662f\u4e00\u4e2a\u5de5\u5177\u65b9\u6cd5\uff0c\u7528\u6765\u6e05\u7406\u5185\u90e8\u7f13\u5b58\u3002\n\u8fd9\u4e2a\u65b9\u6cd5\u518d\u7528\u6237\u8c03\u7528 importlib.invalidate_caches() \u7684\u65f6\u5019\u88ab\u89e6\u53d1\u3002\n\u5982\u679c\u4f60\u60f3\u8ba9URL\u5bfc\u5165\u8005\u91cd\u65b0\u8bfb\u53d6\u94fe\u63a5\u5217\u8868\u7684\u8bdd\u53ef\u4ee5\u4f7f\u7528\u5b83\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u6bd4\u4e0b\u4e24\u79cd\u65b9\u6848\uff08\u4fee\u6539sys.meta_path\u6216\u4f7f\u7528\u4e00\u4e2a\u8def\u5f84\u94a9\u5b50\uff09\u3002\n\u4f7f\u7528sys.meta_path\u7684\u5bfc\u5165\u8005\u53ef\u4ee5\u6309\u7167\u81ea\u5df1\u7684\u9700\u8981\u81ea\u7531\u5904\u7406\u6a21\u5757\u3002\n\u4f8b\u5982\uff0c\u5b83\u4eec\u53ef\u4ee5\u4ece\u6570\u636e\u5e93\u4e2d\u5bfc\u5165\u6216\u4ee5\u4e0d\u540c\u4e8e\u4e00\u822c\u6a21\u5757/\u5305\u5904\u7406\u65b9\u5f0f\u5bfc\u5165\u3002\n\u8fd9\u79cd\u81ea\u7531\u540c\u6837\u610f\u5473\u7740\u5bfc\u5165\u8005\u9700\u8981\u81ea\u5df1\u8fdb\u884c\u5185\u90e8\u7684\u4e00\u4e9b\u7ba1\u7406\u3002\n\u53e6\u5916\uff0c\u57fa\u4e8e\u8def\u5f84\u7684\u94a9\u5b50\u53ea\u662f\u9002\u7528\u4e8e\u5bf9sys.path\u7684\u5904\u7406\u3002\n\u901a\u8fc7\u8fd9\u79cd\u6269\u5c55\u52a0\u8f7d\u7684\u6a21\u5757\u8ddf\u666e\u901a\u65b9\u5f0f\u52a0\u8f7d\u7684\u7279\u6027\u662f\u4e00\u6837\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u5230\u73b0\u5728\u4e3a\u6b62\u4f60\u8fd8\u662f\u4e0d\u662f\u5f88\u660e\u767d\uff0c\u90a3\u4e48\u53ef\u4ee5\u901a\u8fc7\u589e\u52a0\u4e00\u4e9b\u65e5\u5fd7\u6253\u5370\u6765\u6d4b\u8bd5\u4e0b\u672c\u8282\u3002\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import logging\nlogging.basicConfig(level=logging.DEBUG)\nimport urlimport\nurlimport.install_path_hook()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import fib"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sys\nsys.path.append('http://localhost:15000')\nimport fib"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\uff0c\u5efa\u8bae\u4f60\u82b1\u70b9\u65f6\u95f4\u770b\u770b PEP 302\n\u4ee5\u53caimportlib\u7684\u6587\u6863\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 10.12 \u5bfc\u5165\u6a21\u5757\u7684\u540c\u65f6\u4fee\u6539\u6a21\u5757\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u7ed9\u67d0\u4e2a\u5df2\u5b58\u5728\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u6dfb\u52a0\u88c5\u9970\u5668\u3002\n\u4e0d\u8fc7\uff0c\u524d\u63d0\u662f\u8fd9\u4e2a\u6a21\u5757\u5df2\u7ecf\u88ab\u5bfc\u5165\u5e76\u4e14\u88ab\u4f7f\u7528\u8fc7\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u91cc\u95ee\u9898\u7684\u672c\u8d28\u5c31\u662f\u4f60\u60f3\u5728\u6a21\u5757\u88ab\u52a0\u8f7d\u65f6\u6267\u884c\u67d0\u4e2a\u52a8\u4f5c\u3002\n\u53ef\u80fd\u662f\u4f60\u60f3\u5728\u4e00\u4e2a\u6a21\u5757\u88ab\u52a0\u8f7d\u65f6\u89e6\u53d1\u67d0\u4e2a\u56de\u8c03\u51fd\u6570\u6765\u901a\u77e5\u4f60\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u95ee\u9898\u53ef\u4ee5\u4f7f\u752810.11\u5c0f\u8282\u4e2d\u540c\u6837\u7684\u5bfc\u5165\u94a9\u5b50\u673a\u5236\u6765\u5b9e\u73b0\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u53ef\u80fd\u7684\u65b9\u6848\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# postimport.py\nimport importlib\nimport sys\nfrom collections import defaultdict\n\n_post_import_hooks = defaultdict(list)\n\nclass PostImportFinder:\n    def __init__(self):\n        self._skip = set()\n\n    def find_module(self, fullname, path=None):\n        if fullname in self._skip:\n            return None\n        self._skip.add(fullname)\n        return PostImportLoader(self)\n\nclass PostImportLoader:\n    def __init__(self, finder):\n        self._finder = finder\n\n    def load_module(self, fullname):\n        importlib.import_module(fullname)\n        module = sys.modules[fullname]\n        for func in _post_import_hooks[fullname]:\n            func(module)\n        self._finder._skip.remove(fullname)\n        return module\n\ndef when_imported(fullname):\n    def decorate(func):\n        if fullname in sys.modules:\n            func(sys.modules[fullname])\n        else:\n            _post_import_hooks[fullname].append(func)\n        return func\n    return decorate\n\nsys.meta_path.insert(0, PostImportFinder())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u6837\uff0c\u4f60\u5c31\u53ef\u4ee5\u4f7f\u7528 when_imported() \u88c5\u9970\u5668\u4e86\uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from postimport import when_imported\n@when_imported('threading')\ndef warn_threads(mod):\n    print('Threads? Are you crazy?')\nimport threading"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f5c\u4e3a\u4e00\u4e2a\u66f4\u5b9e\u9645\u7684\u4f8b\u5b50\uff0c\u4f60\u53ef\u80fd\u60f3\u5728\u5df2\u5b58\u5728\u7684\u5b9a\u4e49\u4e0a\u9762\u6dfb\u52a0\u88c5\u9970\u5668\uff0c\u5982\u4e0b\u6240\u793a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from functools import wraps\nfrom postimport import when_imported\n\ndef logged(func):\n    @wraps(func)\n    def wrapper(*args, **kwargs):\n        print('Calling', func.__name__, args, kwargs)\n        return func(*args, **kwargs)\n    return wrapper\n\n# Example\n@when_imported('math')\ndef add_logging(mod):\n    mod.cos = logged(mod.cos)\n    mod.sin = logged(mod.sin)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u8282\u6280\u672f\u4f9d\u8d56\u4e8e10.11\u5c0f\u8282\u4e2d\u8bb2\u8ff0\u8fc7\u7684\u5bfc\u5165\u94a9\u5b50\uff0c\u5e76\u7a0d\u4f5c\u4fee\u6539\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "@when_imported \u88c5\u9970\u5668\u7684\u4f5c\u7528\u662f\u6ce8\u518c\u5728\u5bfc\u5165\u65f6\u88ab\u6fc0\u6d3b\u7684\u5904\u7406\u5668\u51fd\u6570\u3002\n\u8be5\u88c5\u9970\u5668\u68c0\u67e5sys.modules\u6765\u67e5\u770b\u6a21\u5757\u662f\u5426\u771f\u7684\u5df2\u7ecf\u88ab\u52a0\u8f7d\u4e86\u3002\n\u5982\u679c\u662f\u7684\u8bdd\uff0c\u8be5\u5904\u7406\u5668\u88ab\u7acb\u5373\u8c03\u7528\u3002\u4e0d\u7136\uff0c\u5904\u7406\u5668\u88ab\u6dfb\u52a0\u5230 _post_import_hooks \u5b57\u5178\u4e2d\u7684\u4e00\u4e2a\u5217\u8868\u4e2d\u53bb\u3002\n_post_import_hooks \u7684\u4f5c\u7528\u5c31\u662f\u6536\u96c6\u6240\u6709\u7684\u4e3a\u6bcf\u4e2a\u6a21\u5757\u6ce8\u518c\u7684\u5904\u7406\u5668\u5bf9\u8c61\u3002\n\u4e00\u4e2a\u6a21\u5757\u53ef\u4ee5\u6ce8\u518c\u591a\u4e2a\u5904\u7406\u5668\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u8ba9\u6a21\u5757\u5bfc\u5165\u540e\u89e6\u53d1\u6dfb\u52a0\u7684\u52a8\u4f5c\uff0cPostImportFinder \u7c7b\u88ab\u8bbe\u7f6e\u4e3asys.meta_path\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\n\u5b83\u4f1a\u6355\u83b7\u6240\u6709\u6a21\u5757\u5bfc\u5165\u64cd\u4f5c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u8282\u4e2d\u7684 PostImportFinder \u7684\u4f5c\u7528\u5e76\u4e0d\u662f\u52a0\u8f7d\u6a21\u5757\uff0c\u800c\u662f\u81ea\u5e26\u5bfc\u5165\u5b8c\u6210\u540e\u89e6\u53d1\u76f8\u5e94\u7684\u52a8\u4f5c\u3002\n\u5b9e\u9645\u7684\u5bfc\u5165\u88ab\u59d4\u6d3e\u7ed9\u4f4d\u4e8esys.meta_path\u4e2d\u7684\u5176\u4ed6\u67e5\u627e\u5668\u3002\nPostImportLoader \u7c7b\u4e2d\u7684 imp.import_module() \u51fd\u6570\u88ab\u9012\u5f52\u7684\u8c03\u7528\u3002\n\u4e3a\u4e86\u907f\u514d\u9677\u5165\u65e0\u7ebf\u5faa\u73af\uff0cPostImportFinder \u4fdd\u6301\u4e86\u4e00\u4e2a\u6240\u6709\u88ab\u52a0\u8f7d\u8fc7\u7684\u6a21\u5757\u96c6\u5408\u3002\n\u5982\u679c\u4e00\u4e2a\u6a21\u5757\u540d\u5b58\u5728\u5c31\u4f1a\u76f4\u63a5\u88ab\u5ffd\u7565\u6389\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u4e00\u4e2a\u6a21\u5757\u88ab imp.import_module() \u52a0\u8f7d\u540e\uff0c\n\u6240\u6709\u5728_post_import_hooks\u88ab\u6ce8\u518c\u7684\u5904\u7406\u5668\u88ab\u8c03\u7528\uff0c\u4f7f\u7528\u65b0\u52a0\u8f7d\u6a21\u5757\u4f5c\u4e3a\u4e00\u4e2a\u53c2\u6570\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684\u662f\u672c\u673a\u4e0d\u9002\u7528\u4e8e\u90a3\u4e9b\u901a\u8fc7 imp.reload() \u88ab\u663e\u5f0f\u52a0\u8f7d\u7684\u6a21\u5757\u3002\n\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4f60\u52a0\u8f7d\u4e00\u4e2a\u4e4b\u524d\u5df2\u88ab\u52a0\u8f7d\u8fc7\u7684\u6a21\u5757\uff0c\u90a3\u4e48\u5bfc\u5165\u5904\u7406\u5668\u5c06\u4e0d\u4f1a\u518d\u88ab\u89e6\u53d1\u3002\n\u53e6\u5916\uff0c\u8981\u662f\u4f60\u4ecesys.modules\u4e2d\u5220\u9664\u6a21\u5757\u7136\u540e\u518d\u91cd\u65b0\u5bfc\u5165\uff0c\u5904\u7406\u5668\u53c8\u4f1a\u518d\u4e00\u6b21\u89e6\u53d1\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u66f4\u591a\u5173\u4e8e\u5bfc\u5165\u540e\u94a9\u5b50\u4fe1\u606f\u8bf7\u53c2\u8003 PEP 369."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 10.13 \u5b89\u88c5\u79c1\u6709\u7684\u5305\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u8981\u5b89\u88c5\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5305\uff0c\u4f46\u662f\u6ca1\u6709\u6743\u9650\u5c06\u5b83\u5b89\u88c5\u5230\u7cfb\u7edfPython\u5e93\u4e2d\u53bb\u3002\n\u6216\u8005\uff0c\u4f60\u53ef\u80fd\u60f3\u8981\u5b89\u88c5\u4e00\u4e2a\u4f9b\u81ea\u5df1\u4f7f\u7528\u7684\u5305\uff0c\u800c\u4e0d\u662f\u7cfb\u7edf\u4e0a\u9762\u6240\u6709\u7528\u6237\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Python\u6709\u4e00\u4e2a\u7528\u6237\u5b89\u88c5\u76ee\u5f55\uff0c\u901a\u5e38\u7c7b\u4f3c\u201d~/.local/lib/python3.3/site-packages\u201d\u3002\n\u8981\u5f3a\u5236\u5728\u8fd9\u4e2a\u76ee\u5f55\u4e2d\u5b89\u88c5\u5305\uff0c\u53ef\u4f7f\u7528\u5b89\u88c5\u9009\u9879\u201c\u2013user\u201d\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "python3 setup.py install --user"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6216\u8005"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "pip install --user packagename"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728sys.path\u4e2d\u7528\u6237\u7684\u201csite-packages\u201d\u76ee\u5f55\u4f4d\u4e8e\u7cfb\u7edf\u7684\u201csite-packages\u201d\u76ee\u5f55\u4e4b\u524d\u3002\n\u56e0\u6b64\uff0c\u4f60\u5b89\u88c5\u5728\u91cc\u9762\u7684\u5305\u5c31\u6bd4\u7cfb\u7edf\u5df2\u5b89\u88c5\u7684\u5305\u4f18\u5148\u7ea7\u9ad8\n\uff08\u5c3d\u7ba1\u5e76\u4e0d\u603b\u662f\u8fd9\u6837\uff0c\u8981\u53d6\u51b3\u4e8e\u7b2c\u4e09\u65b9\u5305\u7ba1\u7406\u5668\uff0c\u6bd4\u5982distribute\u6216pip\uff09\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u5e38\u5305\u4f1a\u88ab\u5b89\u88c5\u5230\u7cfb\u7edf\u7684site-packages\u76ee\u5f55\u4e2d\u53bb\uff0c\u8def\u5f84\u7c7b\u4f3c\u201c/usr/local/lib/python3.3/site-packages\u201d\u3002\n\u4e0d\u8fc7\uff0c\u8fd9\u6837\u505a\u9700\u8981\u6709\u7ba1\u7406\u5458\u6743\u9650\u5e76\u4e14\u4f7f\u7528sudo\u547d\u4ee4\u3002\n\u5c31\u7b97\u4f60\u6709\u8fd9\u6837\u7684\u6743\u9650\u53bb\u6267\u884c\u547d\u4ee4\uff0c\u4f7f\u7528sudo\u53bb\u5b89\u88c5\u4e00\u4e2a\u65b0\u7684\uff0c\u53ef\u80fd\u6ca1\u6709\u88ab\u9a8c\u8bc1\u8fc7\u7684\u5305\u6709\u65f6\u5019\u4e5f\u4e0d\u5b89\u5168\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5b89\u88c5\u5305\u5230\u7528\u6237\u76ee\u5f55\u4e2d\u901a\u5e38\u662f\u4e00\u4e2a\u6709\u6548\u7684\u65b9\u6848\uff0c\u5b83\u5141\u8bb8\u4f60\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5b89\u88c5\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53e6\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u865a\u62df\u73af\u5883\uff0c\u8fd9\u4e2a\u6211\u4eec\u5728\u4e0b\u4e00\u8282\u4f1a\u8bb2\u5230\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 10.14 \u521b\u5efa\u65b0\u7684Python\u73af\u5883\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Python\u73af\u5883\uff0c\u7528\u6765\u5b89\u88c5\u6a21\u5757\u548c\u5305\u3002\n\u4e0d\u8fc7\uff0c\u4f60\u4e0d\u60f3\u5b89\u88c5\u4e00\u4e2a\u65b0\u7684Python\u514b\u9686\uff0c\u4e5f\u4e0d\u60f3\u5bf9\u7cfb\u7edfPython\u73af\u5883\u4ea7\u751f\u5f71\u54cd\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u53ef\u4ee5\u4f7f\u7528 pyvenv \u547d\u4ee4\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u201c\u865a\u62df\u201d\u73af\u5883\u3002\n\u8fd9\u4e2a\u547d\u4ee4\u88ab\u5b89\u88c5\u5728Python\u89e3\u91ca\u5668\u540c\u4e00\u76ee\u5f55\uff0c\u6216Windows\u4e0a\u9762\u7684Scripts\u76ee\u5f55\u4e2d\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "bash % pyvenv Spam\nbash %"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f20\u7ed9 pyvenv \u547d\u4ee4\u7684\u540d\u5b57\u662f\u5c06\u8981\u88ab\u521b\u5efa\u7684\u76ee\u5f55\u540d\u3002\u5f53\u88ab\u521b\u5efa\u540e\uff0cSpan\u76ee\u5f55\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "bash % cd Spam\nbash % ls\nbin include lib pyvenv.cfg\nbash %"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728bin\u76ee\u5f55\u4e2d\uff0c\u4f60\u4f1a\u627e\u5230\u4e00\u4e2a\u53ef\u4ee5\u4f7f\u7528\u7684Python\u89e3\u91ca\u5668\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from pprint import pprint\nimport sys\npprint(sys.path)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u89e3\u91ca\u5668\u7684\u7279\u70b9\u5c31\u662f\u4ed6\u7684site-packages\u76ee\u5f55\u88ab\u8bbe\u7f6e\u4e3a\u65b0\u521b\u5efa\u7684\u73af\u5883\u3002\n\u5982\u679c\u4f60\u8981\u5b89\u88c5\u7b2c\u4e09\u65b9\u5305\uff0c\u5b83\u4eec\u4f1a\u88ab\u5b89\u88c5\u5728\u90a3\u91cc\uff0c\u800c\u4e0d\u662f\u901a\u5e38\u7cfb\u7edf\u7684site-packages\u76ee\u5f55\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u521b\u5efa\u865a\u62df\u73af\u5883\u901a\u5e38\u662f\u4e3a\u4e86\u5b89\u88c5\u548c\u7ba1\u7406\u7b2c\u4e09\u65b9\u5305\u3002\n\u6b63\u5982\u4f60\u5728\u4f8b\u5b50\u4e2d\u770b\u5230\u7684\u90a3\u6837\uff0csys.path \u53d8\u91cf\u5305\u542b\u6765\u81ea\u4e8e\u7cfb\u7edfPython\u7684\u76ee\u5f55\uff0c\n\u800c site-packages\u76ee\u5f55\u5df2\u7ecf\u88ab\u91cd\u5b9a\u4f4d\u5230\u4e00\u4e2a\u65b0\u7684\u76ee\u5f55\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6709\u4e86\u4e00\u4e2a\u65b0\u7684\u865a\u62df\u73af\u5883\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u5b89\u88c5\u4e00\u4e2a\u5305\u7ba1\u7406\u5668\uff0c\u6bd4\u5982distribute\u6216pip\u3002\n\u4f46\u5b89\u88c5\u8fd9\u6837\u7684\u5de5\u5177\u548c\u5305\u7684\u65f6\u5019\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u4f60\u4f7f\u7528\u7684\u662f\u865a\u62df\u73af\u5883\u7684\u89e3\u91ca\u5668\u3002\n\u5b83\u4f1a\u5c06\u5305\u5b89\u88c5\u5230\u65b0\u521b\u5efa\u7684site-packages\u76ee\u5f55\u4e2d\u53bb\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c3d\u7ba1\u4e00\u4e2a\u865a\u62df\u73af\u5883\u770b\u4e0a\u53bb\u662fPython\u5b89\u88c5\u7684\u4e00\u4e2a\u590d\u5236\uff0c\n\u4e0d\u8fc7\u5b83\u5b9e\u9645\u4e0a\u53ea\u5305\u542b\u4e86\u5c11\u91cf\u51e0\u4e2a\u6587\u4ef6\u548c\u4e00\u4e9b\u7b26\u53f7\u94fe\u63a5\u3002\n\u6240\u6709\u6807\u51c6\u5e93\u51fd\u6587\u4ef6\u548c\u53ef\u6267\u884c\u89e3\u91ca\u5668\u90fd\u6765\u81ea\u539f\u6765\u7684Python\u5b89\u88c5\u3002\n\u56e0\u6b64\uff0c\u521b\u5efa\u8fd9\u6837\u7684\u73af\u5883\u662f\u5f88\u5bb9\u6613\u7684\uff0c\u5e76\u4e14\u51e0\u4e4e\u4e0d\u4f1a\u6d88\u8017\u673a\u5668\u8d44\u6e90\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u865a\u62df\u73af\u5883\u662f\u7a7a\u7684\uff0c\u4e0d\u5305\u542b\u4efb\u4f55\u989d\u5916\u7684\u7b2c\u4e09\u65b9\u5e93\u3002\u5982\u679c\u4f60\u60f3\u5c06\u4e00\u4e2a\u5df2\u7ecf\u5b89\u88c5\u7684\u5305\u4f5c\u4e3a\u865a\u62df\u73af\u5883\u7684\u4e00\u90e8\u5206\uff0c\n\u53ef\u4ee5\u4f7f\u7528\u201c\u2013system-site-packages\u201d\u9009\u9879\u6765\u521b\u5efa\u865a\u62df\u73af\u5883\uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "bash % pyvenv --system-site-packages Spam\nbash %"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8ddf\u591a\u5173\u4e8e pyvenv \u548c\u865a\u62df\u73af\u5883\u7684\u4fe1\u606f\u53ef\u4ee5\u53c2\u8003\nPEP 405."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 10.15 \u5206\u53d1\u5305\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u5df2\u7ecf\u7f16\u5199\u4e86\u4e00\u4e2a\u6709\u7528\u7684\u5e93\uff0c\u60f3\u5c06\u5b83\u5206\u4eab\u7ed9\u5176\u4ed6\u4eba\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u5206\u53d1\u4f60\u7684\u4ee3\u7801\uff0c\u7b2c\u4e00\u4ef6\u4e8b\u5c31\u662f\u7ed9\u5b83\u4e00\u4e2a\u552f\u4e00\u7684\u540d\u5b57\uff0c\u5e76\u4e14\u6e05\u7406\u5b83\u7684\u76ee\u5f55\u7ed3\u6784\u3002\n\u4f8b\u5982\uff0c\u4e00\u4e2a\u5178\u578b\u7684\u51fd\u6570\u5e93\u5305\u4f1a\u7c7b\u4f3c\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "projectname/\n    README.txt\n    Doc/\n        documentation.txt\n    projectname/\n        __init__.py\n        foo.py\n        bar.py\n        utils/\n            __init__.py\n            spam.py\n            grok.py\n    examples/\n        helloworld.py\n        ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u8ba9\u4f60\u7684\u5305\u53ef\u4ee5\u53d1\u5e03\u51fa\u53bb\uff0c\u9996\u5148\u4f60\u8981\u7f16\u5199\u4e00\u4e2a setup.py \uff0c\u7c7b\u4f3c\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# setup.py\nfrom distutils.core import setup\n\nsetup(name='projectname',\n    version='1.0',\n    author='Your Name',\n    author_email='you@youraddress.com',\n    url='http://www.you.com/projectname',\n    packages=['projectname', 'projectname.utils'],\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u4e00\u6b65\uff0c\u5c31\u662f\u521b\u5efa\u4e00\u4e2a MANIFEST.in \u6587\u4ef6\uff0c\u5217\u51fa\u6240\u6709\u5728\u4f60\u7684\u5305\u4e2d\u9700\u8981\u5305\u542b\u8fdb\u6765\u7684\u975e\u6e90\u7801\u6587\u4ef6\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# MANIFEST.in\ninclude *.txt\nrecursive-include examples *\nrecursive-include Doc *"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u786e\u4fdd setup.py \u548c MANIFEST.in \u6587\u4ef6\u653e\u5728\u4f60\u7684\u5305\u7684\u6700\u9876\u7ea7\u76ee\u5f55\u4e2d\u3002\n\u4e00\u65e6\u4f60\u5df2\u7ecf\u505a\u4e86\u8fd9\u4e9b\uff0c\u4f60\u5c31\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u6267\u884c\u547d\u4ee4\u6765\u521b\u5efa\u4e00\u4e2a\u6e90\u7801\u5206\u53d1\u5305\u4e86\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "% bash python3 setup.py sdist"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5b83\u4f1a\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u6bd4\u5982\u201dprojectname-1.0.zip\u201d \u6216 \u201cprojectname-1.0.tar.gz\u201d,\n\u5177\u4f53\u4f9d\u8d56\u4e8e\u4f60\u7684\u7cfb\u7edf\u5e73\u53f0\u3002\u5982\u679c\u4e00\u5207\u6b63\u5e38\uff0c\n\u8fd9\u4e2a\u6587\u4ef6\u5c31\u53ef\u4ee5\u53d1\u9001\u7ed9\u522b\u4eba\u4f7f\u7528\u6216\u8005\u4e0a\u4f20\u81f3 Python Package Index."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u7eafPython\u4ee3\u7801\uff0c\u7f16\u5199\u4e00\u4e2a\u666e\u901a\u7684 setup.py \u6587\u4ef6\u901a\u5e38\u5f88\u7b80\u5355\u3002\n\u4e00\u4e2a\u53ef\u80fd\u7684\u95ee\u9898\u662f\u4f60\u5fc5\u987b\u624b\u52a8\u5217\u51fa\u6240\u6709\u6784\u6210\u5305\u6e90\u7801\u7684\u5b50\u76ee\u5f55\u3002\n\u4e00\u4e2a\u5e38\u89c1\u9519\u8bef\u5c31\u662f\u4ec5\u4ec5\u53ea\u5217\u51fa\u4e00\u4e2a\u5305\u7684\u6700\u9876\u7ea7\u76ee\u5f55\uff0c\u5fd8\u8bb0\u4e86\u5305\u542b\u5305\u7684\u5b50\u7ec4\u4ef6\u3002\n\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728 setup.py \u4e2d\u5bf9\u4e8e\u5305\u7684\u8bf4\u660e\u5305\u542b\u4e86\u5217\u8868\npackages=['projectname', 'projectname.utils']"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5927\u90e8\u5206Python\u7a0b\u5e8f\u5458\u90fd\u77e5\u9053\uff0c\u6709\u5f88\u591a\u7b2c\u4e09\u65b9\u5305\u7ba1\u7406\u5668\u4f9b\u9009\u62e9\uff0c\u5305\u62ecsetuptools\u3001distribute\u7b49\u7b49\u3002\n\u6709\u4e9b\u662f\u4e3a\u4e86\u66ff\u4ee3\u6807\u51c6\u5e93\u4e2d\u7684distutils\u3002\u6ce8\u610f\u5982\u679c\u4f60\u4f9d\u8d56\u8fd9\u4e9b\u5305\uff0c\n\u7528\u6237\u53ef\u80fd\u4e0d\u80fd\u5b89\u88c5\u4f60\u7684\u8f6f\u4ef6\uff0c\u9664\u975e\u4ed6\u4eec\u5df2\u7ecf\u4e8b\u5148\u5b89\u88c5\u8fc7\u6240\u9700\u8981\u7684\u5305\u7ba1\u7406\u5668\u3002\n\u6b63\u56e0\u5982\u6b64\uff0c\u4f60\u66f4\u5e94\u8be5\u65f6\u523b\u8bb0\u4f4f\u8d8a\u7b80\u5355\u8d8a\u597d\u7684\u9053\u7406\u3002\n\u6700\u597d\u8ba9\u4f60\u7684\u4ee3\u7801\u4f7f\u7528\u6807\u51c6\u7684Python 3\u5b89\u88c5\u3002\n\u5982\u679c\u5176\u4ed6\u5305\u4e5f\u9700\u8981\u7684\u8bdd\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u53ef\u9009\u9879\u6765\u652f\u6301\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u6d89\u53ca\u5230C\u6269\u5c55\u7684\u4ee3\u7801\u6253\u5305\u4e0e\u5206\u53d1\u5c31\u66f4\u590d\u6742\u70b9\u4e86\u3002\n\u7b2c15\u7ae0\u5bf9\u5173\u4e8eC\u6269\u5c55\u7684\u8fd9\u65b9\u9762\u77e5\u8bc6\u6709\u4e00\u4e9b\u8be6\u7ec6\u8bb2\u89e3\uff0c\u7279\u522b\u662f\u572815.2\u5c0f\u8282\u4e2d\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}